MapController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace common\widgets\map;
  3. use Yii;
  4. use yii\web\Controller;
  5. /**
  6. * Class MapController
  7. * @package common\widgets\map
  8. * @author jianyan74 <751393839@qq.com>
  9. */
  10. class MapController extends Controller
  11. {
  12. /**
  13. * @param $type
  14. * @param $secret_key
  15. * @param string $lng
  16. * @param string $lat
  17. * @return string
  18. */
  19. public function actionMap($type, $lng = '', $lat = '', $zoom = 12, $boxId = '', $defaultSearchAddress = '')
  20. {
  21. $this->layout = '@backend/views/layouts/blank';
  22. $secret_key = '';
  23. // 注册js
  24. $this->registerViewJs($type, $secret_key);
  25. // 高德 code
  26. $mapAMapCode = Yii::$app->services->config->backendConfig('map_amap_code');
  27. return $this->render('@common/widgets/map/views/map/' . $type, [
  28. 'lng' => $lng,
  29. 'lat' => $lat,
  30. 'zoom' => $zoom,
  31. 'boxId' => $boxId,
  32. 'mapAMapCode' => $mapAMapCode,
  33. 'defaultSearchAddress' => $defaultSearchAddress,
  34. ]);
  35. }
  36. /**
  37. * @param $type
  38. * @param string $lng
  39. * @param string $lat
  40. * @param int $zoom
  41. * @param int $boxId
  42. * @return string
  43. */
  44. public function actionMapView($type, $lng = '', $lat = '', $label = '', $zoom = 12)
  45. {
  46. $secret_key = '';
  47. // 注册js
  48. $this->registerViewJs($type, $secret_key);
  49. return $this->render('@common/widgets/map/views/map/detail/' . $type, [
  50. 'lng' => $lng,
  51. 'lat' => $lat,
  52. 'label' => $label,
  53. 'zoom' => $zoom,
  54. ]);
  55. }
  56. /**
  57. * @param $type
  58. * @param string $lng
  59. * @param string $lat
  60. * @param int $zoom
  61. * @param int $boxId
  62. * @return string
  63. */
  64. public function actionRidingRoute($type, $lng = '', $lat = '', $label = '', $lng2 = '', $lat2 = '', $label2 = '', $zoom = 12)
  65. {
  66. $secret_key = '';
  67. // 注册js
  68. $this->registerViewJs($type, $secret_key);
  69. return $this->render('@common/widgets/map/views/map/route/' . $type, [
  70. 'lng' => $lng,
  71. 'lat' => $lat,
  72. 'label' => $label,
  73. 'lng2' => $lng2,
  74. 'lat2' => $lat2,
  75. 'label2' => $label2,
  76. 'zoom' => $zoom,
  77. ]);
  78. }
  79. /**
  80. * 手动输入
  81. *
  82. * @param string $lng
  83. * @param string $lat
  84. * @param int $boxId
  85. * @return string
  86. */
  87. public function actionInput($lng = '', $lat = '', $boxId = 12)
  88. {
  89. return $this->renderAjax('@common/widgets/map/views/map/input', [
  90. 'lng' => $lng,
  91. 'lat' => $lat,
  92. 'boxId' => $boxId,
  93. ]);
  94. }
  95. /**
  96. * @throws \yii\base\InvalidConfigException
  97. */
  98. public function registerViewJs($type, $secret_key)
  99. {
  100. $view = $this->view;
  101. switch ($type) {
  102. case 'baidu' :
  103. empty($secret_key) && $secret_key = Yii::$app->services->config->backendConfig('map_baidu_ak');
  104. $view->registerJsFile('https://api.map.baidu.com/api?v=2.0&ak=' . $secret_key);
  105. break;
  106. case 'amap' :
  107. empty($secret_key) && $secret_key = Yii::$app->services->config->backendConfig('map_amap_key');
  108. $view->registerJsFile('https://webapi.amap.com/maps?v=1.4.11&plugin=AMap.ToolBar,AMap.Autocomplete,AMap.PlaceSearch,AMap.Geocoder&key=' . $secret_key);
  109. $view->registerJsFile('https://webapi.amap.com/ui/1.0/main.js?v=1.0.11');
  110. break;
  111. case 'tencent' :
  112. empty($secret_key) && $secret_key = Yii::$app->services->config->backendConfig('map_tencent_key');
  113. $view->registerJsFile('https://map.qq.com/api/js?v=2.exp&libraries=place&key=' . $secret_key);
  114. break;
  115. }
  116. $view->registerCss(<<<Css
  117. #container {
  118. position: absolute;
  119. left: 0;
  120. top: 0;
  121. right: 0;
  122. bottom: 0;
  123. }
  124. .search {
  125. position: absolute;
  126. width: 400px;
  127. top: 0;
  128. left: 50%;
  129. padding: 5px;
  130. margin-left: -200px;
  131. }
  132. Css
  133. );
  134. }
  135. }
粤ICP备19079148号