Map.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * Get assets required by this field.
  16. *
  17. * @return void
  18. */
  19. public static function requireAssets()
  20. {
  21. $keys = config('admin.map.keys');
  22. switch (static::getUsingMap()) {
  23. case 'tencent':
  24. $js = '//map.qq.com/api/js?v=2.exp&key='.($keys['tencent'] ?? env('TENCENT_MAP_API_KEY'));
  25. break;
  26. case 'google':
  27. $js = '//maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key='.($keys['google'] ?? env('GOOGLE_API_KEY'));
  28. break;
  29. case 'yandex':
  30. $js = '//api-maps.yandex.ru/2.1/?lang=ru_RU';
  31. break;
  32. case 'baidu':
  33. $js = '//api.map.baidu.com/api?v=2.0&ak='.($keys['baidu'] ?? env('BAIDU_MAP_API_KEY'));
  34. break;
  35. default:
  36. $js = '//api.map.baidu.com/api?v=2.0&ak='.($keys['baidu'] ?? env('BAIDU_MAP_API_KEY'));
  37. }
  38. Admin::js($js);
  39. }
  40. public function __construct($column, $arguments)
  41. {
  42. $this->column['lat'] = (string) $column;
  43. $this->column['lng'] = (string) $arguments[0];
  44. array_shift($arguments);
  45. $this->label = $this->formatLabel($arguments);
  46. /*
  47. * Google map is blocked in mainland China
  48. * people in China can use Tencent map instead(;
  49. */
  50. switch (static::getUsingMap()) {
  51. case 'tencent':
  52. $this->tencent();
  53. break;
  54. case 'google':
  55. $this->google();
  56. break;
  57. case 'yandex':
  58. $this->yandex();
  59. break;
  60. case 'baidu':
  61. default:
  62. $this->baidu();
  63. }
  64. }
  65. protected static function getUsingMap()
  66. {
  67. return config('admin.map.provider') ?: config('admin.map_provider');
  68. }
  69. public function google()
  70. {
  71. return $this->addVariables(['type' => 'google']);
  72. }
  73. public function tencent()
  74. {
  75. return $this->addVariables(['type' => 'tencent']);
  76. }
  77. public function yandex()
  78. {
  79. return $this->addVariables(['type' => 'yandex']);
  80. }
  81. public function baidu()
  82. {
  83. return $this->addVariables(['type' => 'baidu', 'searchId' => 'bdmap'.Str::random()]);
  84. }
  85. }