Map.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace common\widgets\map;
  3. use Yii;
  4. use yii\helpers\Html;
  5. use yii\widgets\InputWidget;
  6. use common\helpers\StringHelper;
  7. /**
  8. * 地图经纬度选择器
  9. *
  10. * Class Map
  11. * @package common\widgets\map
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class Map extends InputWidget
  15. {
  16. /**
  17. * 默认地址
  18. *
  19. * @var bool
  20. */
  21. public $defaultSearchAddress = '北京';
  22. /**
  23. * 秘钥
  24. *
  25. * @var string
  26. */
  27. public $secret_key = '';
  28. /**
  29. * 申请的安全密钥(高德)
  30. *
  31. * @var string
  32. */
  33. public $secret_code = '';
  34. /**
  35. * 类型
  36. *
  37. * 默认高德
  38. *
  39. * amap 高德
  40. * tencent 腾讯
  41. * baidu 高德
  42. *
  43. * @var string
  44. */
  45. public $type = 'amap';
  46. /**
  47. * @return string
  48. * @throws \Exception
  49. */
  50. public function run()
  51. {
  52. $value = $this->hasModel() ? Html::getAttributeValue($this->model, $this->attribute) : $this->value;
  53. $name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name;
  54. try {
  55. if ($value && !is_array($value)) {
  56. $value = json_decode($value, true);
  57. empty($value) && $value = unserialize($value);
  58. empty($value) && $value = [];
  59. }
  60. } catch (\Exception $e) {
  61. $value = [];
  62. }
  63. // 显示地址
  64. $address = empty($value) ? '' : implode(',', [$value['longitude'] ?? '', $value['latitude'] ?? '']);
  65. $defaultValue = [
  66. 'longitude' => $value['longitude'] ?? '116.456270',
  67. 'latitude' => $value['latitude'] ?? '39.919990',
  68. ];
  69. return $this->render('map/index', [
  70. 'name' => $name,
  71. 'value' => $defaultValue,
  72. 'type' => $this->type,
  73. 'secret_key' => $this->secret_key,
  74. 'secret_code' => $this->secret_code,
  75. 'address' => $address,
  76. 'defaultSearchAddress' => $this->defaultSearchAddress,
  77. 'boxId' => StringHelper::uuid('uniqid')
  78. ]);
  79. }
  80. }
粤ICP备19079148号