com-map.php 9.8 KB

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