Map.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form\Field;
  5. use Illuminate\Support\Str;
  6. class Map extends Field
  7. {
  8. /**
  9. * Column name.
  10. *
  11. * @var array
  12. */
  13. protected $column = [];
  14. /**
  15. * @var string
  16. */
  17. protected $height = '300px';
  18. /**
  19. * Get assets required by this field.
  20. *
  21. * @return void
  22. */
  23. public static function requireAssets()
  24. {
  25. $keys = config('admin.map.keys');
  26. switch (static::getUsingMap()) {
  27. case 'tencent':
  28. $js = '//map.qq.com/api/js?v=2.exp&key='.($keys['tencent'] ?? env('TENCENT_MAP_API_KEY'));
  29. break;
  30. case 'google':
  31. $js = '//maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key='.($keys['google'] ?? env('GOOGLE_API_KEY'));
  32. break;
  33. case 'yandex':
  34. $js = '//api-maps.yandex.ru/2.1/?lang=ru_RU';
  35. break;
  36. case 'amap':
  37. $js = '//webapi.amap.com/maps?v=1.4.15&plugin=AMap.Autocomplete,AMap.PlaceSearch,AMap.Geolocation&key='.($keys['amap'] ?? env('AMAP_API_KEY'));
  38. break;
  39. case 'baidu':
  40. default:
  41. $js = '//api.map.baidu.com/api?v=2.0&ak='.($keys['baidu'] ?? env('BAIDU_MAP_API_KEY'));
  42. }
  43. Admin::js($js);
  44. }
  45. public function __construct($column, $arguments)
  46. {
  47. $this->column['lat'] = (string) $column;
  48. $this->column['lng'] = (string) $arguments[0];
  49. array_shift($arguments);
  50. $this->label = $this->formatLabel($arguments);
  51. /*
  52. * Google map is blocked in mainland China
  53. * people in China can use Tencent map instead(;
  54. */
  55. switch (static::getUsingMap()) {
  56. case 'tencent':
  57. $this->tencent();
  58. break;
  59. case 'google':
  60. $this->google();
  61. break;
  62. case 'yandex':
  63. $this->yandex();
  64. break;
  65. case 'amap':
  66. $this->amap();
  67. break;
  68. case 'baidu':
  69. default:
  70. $this->baidu();
  71. }
  72. }
  73. public function height(string $height)
  74. {
  75. $this->height = $height;
  76. return $this;
  77. }
  78. protected static function getUsingMap()
  79. {
  80. return config('admin.map.provider') ?: config('admin.map_provider');
  81. }
  82. public function google()
  83. {
  84. return $this->addVariables(['type' => 'google']);
  85. }
  86. public function tencent()
  87. {
  88. return $this->addVariables(['type' => 'tencent']);
  89. }
  90. public function yandex()
  91. {
  92. return $this->addVariables(['type' => 'yandex']);
  93. }
  94. public function baidu()
  95. {
  96. return $this->addVariables(['type' => 'baidu', 'searchId' => 'bdmap'.Str::random()]);
  97. }
  98. public function amap()
  99. {
  100. return $this->addVariables(['type' => 'amap', 'searchId' => 'amap'.Str::random()]);
  101. }
  102. protected function getDefaultElementClass()
  103. {
  104. $class = $this->normalizeElementClass($this->column['lat']).$this->normalizeElementClass($this->column['lng']);
  105. return [$class, static::NORMAL_CLASS];
  106. }
  107. public function render()
  108. {
  109. $this->addVariables(['height' => $this->height]);
  110. return parent::render();
  111. }
  112. }