qq-map.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. $config = Yii::$app->services->setting->platform->get('map');
  3. $key = $config['qq_jsapi_key'] ? $config['qq_jsapi_key'] : '2DZBZ-34CWX-5CH45-7TY7A-FNPAJ-SIBDC';
  4. ?>
  5. <template id="qq-map">
  6. <div class="qq-map">
  7. <el-dialog :title="title ? title : '地图展示'"
  8. :visible="show"
  9. @opened="dialogOpened"
  10. @before-close="onBeforeClose"
  11. :show-close="false"
  12. :close-on-click-modal="true">
  13. <el-form label-width="80px" size="small">
  14. <el-row>
  15. <el-col :span="12">
  16. <el-form-item label="地址搜索">
  17. <el-input placeholder="请输入具体地址" v-model="mapKeyword">
  18. <el-button @keyup.enter.native="mapSearch" @click="mapSearch" slot="append"
  19. icon="el-icon-search"></el-button>
  20. </el-input>
  21. </el-form-item>
  22. </el-col>
  23. </el-row>
  24. <el-row>
  25. <el-col :span="12">
  26. <el-form-item label="地址">
  27. <el-input disabled v-model="newAddress"></el-input>
  28. </el-form-item>
  29. </el-col>
  30. <el-col :span="12">
  31. <el-form-item label="纬度|经度">
  32. <el-input disabled v-model="lat_long"></el-input>
  33. </el-form-item>
  34. </el-col>
  35. </el-row>
  36. </el-form>
  37. <div class="com-map" id="container" :style="style"></div>
  38. <span style="height:30px;display:none;" id="city"></span>
  39. <div slot="footer" class="dialog-footer">
  40. <el-button type="info" @click="onCancel">取 消</el-button>
  41. <el-button type="primary" @click="confirm">确 定</el-button>
  42. </div>
  43. </el-dialog>
  44. </div>
  45. </template>
  46. <script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=<?php echo $key;?>"></script>
  47. <script>
  48. Vue.component('qq-map', {
  49. template: '#qq-map',
  50. props: {
  51. width: String,
  52. height: String,
  53. lat: [String, Number],
  54. long: [String, Number],
  55. address: {
  56. type: String,
  57. default: '',
  58. },
  59. title: String,
  60. show: false
  61. },
  62. data() {
  63. return {
  64. longitude: '', // 经度(大)
  65. latitude: '',// 纬度(小)
  66. lat_long: '',
  67. markers: [],
  68. map: null,
  69. searchService: {},
  70. mapKeyword: '',
  71. newAddress: '',
  72. city: '',
  73. newAddressComponents: null
  74. };
  75. },
  76. created() {
  77. },
  78. computed: {
  79. style() {
  80. let width = '100%';
  81. let height = '400px';
  82. if (this.width) {
  83. width = this.width + (isNaN(this.width) ? '' : 'px');
  84. }
  85. if (this.height) {
  86. height = this.height + (isNaN(this.height) ? '' : 'px');
  87. }
  88. return `width:${width};height:${height};`;
  89. },
  90. },
  91. methods: {
  92. dialogOpened() {
  93. //this.newAddress = this.address ? this.address : '';
  94. this.initMap();
  95. },
  96. // 初始化地图
  97. initMap() {
  98. let self = this;
  99. this.newAddress = this.address
  100. this.lat_long = this.lat + '-' + this.long
  101. if(this.lat) {
  102. this.latitude = this.lat;
  103. }
  104. if(this.long){
  105. this.longitude = this.long;
  106. }
  107. let center = new qq.maps.LatLng(this.latitude, this.longitude);// 默认坐标
  108. let container = document.getElementById("container");
  109. self.map = new qq.maps.Map(
  110. container,
  111. {
  112. center: center,
  113. zoom: 13,// 缩放级别
  114. }
  115. );
  116. let citylocation = new qq.maps.CityService({
  117. map: self.map,
  118. complete: function (results) {
  119. console.log('所在位置: ',results);
  120. self.city = results.detail.name;
  121. self.map.setCenter(results.detail.latLng);
  122. let marker = self.setMarker(results.detail.latLng);
  123. self.markers.push(marker);
  124. self.getAddressBylatLong();
  125. self.clickEvent(center);
  126. self.initSearch();
  127. },
  128. error: function(e) {
  129. console.log('吐了',e);
  130. }
  131. });
  132. // 搜索服务 默认获取当前地址
  133. if (!this.lat && !this.long) {
  134. citylocation.searchLocalCity()
  135. } else {
  136. this.latitude = this.lat;
  137. this.longitude = this.long;
  138. this.lat_long = this.lat + ',' + this.long;
  139. // 先这样处理
  140. // let latLng = new qq.maps.LatLng(this.latitude, this.longitude);
  141. // citylocation.searchCityByLatLng(latLng);
  142. citylocation.searchLocalCity()
  143. }
  144. },
  145. // 地图点击事件
  146. clickEvent(center) {
  147. let self = this;
  148. let listener = qq.maps.event.addListener(this.map, 'click', function (event) {
  149. self.longitude = event.latLng.getLng().toFixed(6);
  150. self.latitude = event.latLng.getLat().toFixed(6);
  151. self.lat_long = self.latitude + ',' + self.longitude;
  152. self.getAddressBylatLong();
  153. let coord = new qq.maps.LatLng(self.latitude, self.longitude);
  154. let marker = self.setMarker(coord);
  155. self.markers.push(marker);
  156. });
  157. },
  158. // 根据经纬度获取地址信息
  159. getAddressBylatLong() {
  160. let self = this;
  161. // 根据经纬度查询城市信息
  162. let geocoder = new qq.maps.Geocoder({
  163. complete: function (result) {
  164. self.newAddress = result.detail.address;
  165. self.newAddressComponents = result.detail.addressComponents;
  166. }
  167. });
  168. let coord = new qq.maps.LatLng(self.latitude, self.longitude);
  169. geocoder.getAddress(coord);
  170. },
  171. // 添加标注
  172. setMarker(coord) {
  173. let self = this;
  174. // 添加标注
  175. let marker = new qq.maps.Marker({
  176. map: self.map,
  177. position: coord
  178. });
  179. //获取标记的点击事件
  180. qq.maps.event.addListener(marker, 'click', function (event) {
  181. self.longitude = event.latLng.getLng().toFixed(6);
  182. self.latitude = event.latLng.getLat().toFixed(6);
  183. self.lat_long = self.latitude + ',' + self.longitude;
  184. self.getAddressBylatLong();
  185. });
  186. return marker;
  187. },
  188. // 清除地址坐标
  189. clearOverLays() {
  190. //清除地图上的marker
  191. let overlay;
  192. while (overlay = this.markers.pop()) {
  193. overlay.setMap(null);
  194. }
  195. },
  196. initSearch() {
  197. let self = this;
  198. let latlngBounds = new qq.maps.LatLngBounds();
  199. //设置Poi检索服务,用于本地检索、周边检索
  200. self.searchService = new qq.maps.SearchService({
  201. //设置搜索范围为
  202. location: self.city,
  203. //设置动扩大检索区域。默认值true,会自动检索指定城市以外区域。
  204. autoExtend: true,
  205. //检索成功的回调函数
  206. complete: function (results) {
  207. //设置回调函数参数
  208. let pois = results.detail.pois;
  209. if (!pois) {
  210. alert("输入详细地址搜索更准确");
  211. return false;
  212. }
  213. for (let i = 0, l = pois.length; i < l; i++) {
  214. let poi = pois[i];
  215. //扩展边界范围,用来包含搜索到的Poi点
  216. latlngBounds.extend(poi.latLng);
  217. let marker = self.setMarker(poi.latLng)
  218. marker.setTitle(i + 1);
  219. self.markers.push(marker);
  220. }
  221. //调整地图视野
  222. self.map.fitBounds(latlngBounds);
  223. },
  224. //若服务请求失败,则运行以下函数
  225. error: function () {
  226. alert("出错了。");
  227. }
  228. });
  229. },
  230. // 地址搜索
  231. mapSearch() {
  232. this.clearOverLays();
  233. this.searchService.search(this.mapKeyword);
  234. },
  235. confirm() {
  236. this.$emit('map-submit', {
  237. lat: this.latitude,
  238. long: this.longitude,
  239. address: this.newAddress,
  240. addressComponents: this.newAddressComponents,
  241. });
  242. // this.dialogVisible = false;
  243. },
  244. onBeforeClose() {
  245. },
  246. onCancel() {
  247. this.$emit('on-hide');
  248. }
  249. },
  250. });
  251. </script>