推广小程序后台代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
6.2 KiB

4 months ago
  1. <template>
  2. <div>
  3. <div class="map-box">
  4. <div class="map" ref="map" style="height: 400px" ></div>
  5. <div class="map-search">
  6. <div>
  7. <a-input-search id="place" v-model="searchValue" placeholder="请输入详细地址" enter-button @search="searchAddress(searchValue)" />
  8. </div>
  9. <div>
  10. <ul v-if="kwData.length">
  11. <li v-for="(item, index) in kwData" :key="index" @click="selectKeyword(item)">{{ item.address }}</li>
  12. </ul>
  13. </div>
  14. </div>
  15. </div>
  16. </div>
  17. </template>
  18. <script>
  19. export default {
  20. name: 'TencentMapPicker',
  21. props: {
  22. latitude: {
  23. type: Number|String,
  24. default: 0
  25. },
  26. longitude: {
  27. type: Number|String,
  28. default: 0
  29. }
  30. },
  31. watch: {
  32. latitude(newVal, oldVal) {
  33. console.info(`newVal`,newVal,`oldVal`,oldVal)
  34. if (newVal) {
  35. this.form.latitude = newVal
  36. // this.$nextTick(()=>{
  37. // this.map.setCenter(new window.TMap.LatLng(this.form.latitude, this.form.longitude));
  38. // })
  39. }
  40. },
  41. longitude(newVal, oldVal) {
  42. console.info(`newVal`,newVal,`oldVal`,oldVal)
  43. if (newVal) {
  44. this.form.longitude = newVal
  45. // this.$nextTick(()=>{
  46. // this.map.setCenter(new window.TMap.LatLng(this.form.latitude, this.form.longitude));
  47. // })
  48. }
  49. }
  50. },
  51. data() {
  52. return {
  53. searchValue: '', //地图搜索
  54. form: {
  55. longitude: 116.397128,
  56. latitude: 39.916527
  57. },
  58. kwData:[]
  59. }
  60. },
  61. mounted() {
  62. let that = this
  63. if (that.script) return;
  64. that.script = document.createElement('script');
  65. that.script.type = 'text/javascript';
  66. that.script.src = `//map.qq.com/api/gljs?v=1.exp&libraries=service&key=DGFBZ-JU76M-34A63-6BU47-2FLJH-XLBCN`;
  67. document.head.appendChild(that.script);
  68. that.script.onload = () => {
  69. window.initMap = that.initMap;
  70. that.$nextTick(() => {
  71. that.initMap();
  72. });
  73. };
  74. },
  75. methods: {
  76. // 地图
  77. initMap() {
  78. // 搜索类
  79. this.searchEr = new window.TMap.service.Search({ pageSize: 15 });
  80. // 地图主类
  81. this.map = new window.TMap.Map(this.$refs.map, {
  82. backgroundColor: '#f7f7f7',
  83. mapStyleId: 'style1',
  84. zoom: 14,
  85. mapTypeControlOptions: {
  86. mapTypeIds: [],
  87. },
  88. draggableCursor: 'crosshair',
  89. center: new window.TMap.LatLng(this.form.latitude, this.form.longitude),
  90. });
  91. // 图层类
  92. this.markerLayer = new window.TMap.MultiMarker({
  93. map: this.map,
  94. geometries: [],
  95. });
  96. // 地址逆解析
  97. this.geocoder = new window.TMap.service.Geocoder();
  98. const setMarker = () => {
  99. const latlng = new window.TMap.LatLng(this.form.latitude, this.form.longitude);
  100. this.markerLayer.setGeometries([]);
  101. let geometries = this.markerLayer.getGeometries();
  102. geometries.push({
  103. id: '-1',
  104. position: latlng,
  105. });
  106. this.markerLayer.updateGeometries(geometries);
  107. };
  108. this.map.on('click', (e) => {
  109. this.form.longitude = e.latLng.getLng(); // 经度
  110. this.form.latitude = e.latLng.getLat(); // 纬度
  111. setMarker();
  112. this.getAddressFormat();
  113. });
  114. if (this.form.longitude) {
  115. this.map.setCenter(new window.TMap.LatLng(this.form.latitude, this.form.longitude));
  116. setMarker();
  117. }
  118. },
  119. selectKeyword(event){
  120. this.map.setCenter(new window.TMap.LatLng(event.location.lat, event.location.lng));
  121. this.kwData = []
  122. },
  123. // 地图搜索
  124. searchAddress(keyword = '') {
  125. if (!keyword) return;
  126. this.$jsonp('https://apis.map.qq.com/ws/place/v1/suggestion', {
  127. key: 'DGFBZ-JU76M-34A63-6BU47-2FLJH-XLBCN',
  128. output: 'jsonp',
  129. keyword: keyword,
  130. }).then((res) => {
  131. console.log(`res`,res)
  132. if (res.status === 0) {
  133. this.kwData = res.data
  134. } else {
  135. this.kwData = []
  136. }
  137. }).catch((e) => {
  138. console.log(e)
  139. })
  140. this.markerLayer.setGeometries([]);
  141. this.searchEr.searchRectangle({
  142. keyword,
  143. bounds: this.map.getBounds(),
  144. })
  145. .then((result) => {
  146. console.info(`result`,result)
  147. const { location = {} } = result.data[0] || {};
  148. const { lat = 39.916527, lng = 116.397128 } = location;
  149. this.map.setCenter(new window.TMap.LatLng(lat, lng));
  150. result.data.forEach((item, index) => {
  151. let geometries = this.markerLayer.getGeometries();
  152. // 点标注数据数组
  153. geometries.push({
  154. id: String(index),
  155. position: item.location,
  156. });
  157. // 绘制地点标注
  158. this.markerLayer.updateGeometries(geometries);
  159. });
  160. });
  161. },
  162. getAddressFormat() {
  163. const { longitude, latitude } = this.form;
  164. this.geocoder
  165. .getAddress({
  166. location: new window.TMap.LatLng(latitude, longitude),
  167. })
  168. .then((res) => {
  169. const {
  170. formatted_addresses: { recommend = '' },
  171. } = res.result || {};
  172. this.searchValue = recommend
  173. this.form.hotelDetailAddress = recommend;
  174. this.$emit('onLocationSelected', this.form);
  175. console.log(this.form);
  176. });
  177. },
  178. }
  179. }
  180. </script>
  181. <style scoped>
  182. .map-box {
  183. position: relative;
  184. }
  185. .map-search {
  186. z-index: 1000;
  187. display: flex;
  188. flex-direction: column;
  189. position: absolute;
  190. top: 20px;
  191. left: 20px;
  192. width: 300px;
  193. }
  194. button {
  195. border-radius: 0;
  196. }
  197. .icons {
  198. display: flex;
  199. align-items: center;
  200. justify-content: space-between;
  201. }
  202. .time {
  203. margin-top: 15px;
  204. width: 100%;
  205. font-size: 12px;
  206. flex-wrap: wrap;
  207. height: 70%;
  208. }
  209. ul{
  210. background: white;
  211. list-style-type: none;
  212. padding-left: 0px;
  213. height: 280px;
  214. overflow: scroll;
  215. }
  216. li{
  217. padding-left: 6px;
  218. font-size: 10px;
  219. text-align: left;
  220. height: 40px;
  221. line-height: 15px;
  222. display: flex;
  223. align-items: center;
  224. }
  225. li:hover{
  226. cursor: pointer;
  227. background: #f5f5f5;
  228. }
  229. </style>