map.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <div>
  3. <el-dialog title="地图选点" v-model="isShowDialog" width="900px" append-to-body>
  4. <div class="map-container">
  5. <div class="coordinate-search">
  6. <el-tooltip class="box-item" effect="dark" content="点击放大镜或回车按键检索地址" placement="top-start">
  7. <el-input v-model="searchKeyword" placeholder="搜索地名" @keyup.enter.native="searchByKeyword(searchKeyword)">
  8. <template #append>
  9. <el-button :icon="Search" @click="searchByKeyword(searchKeyword)" />
  10. </template>
  11. </el-input>
  12. </el-tooltip>
  13. <el-input v-model="lng" placeholder="经度" />
  14. <div>-</div>
  15. <el-input v-model="lat" placeholder="纬度" />
  16. <el-button @click="searchByCoordinate" type="primary">搜索</el-button>
  17. </div>
  18. <div class="map" ref="mapContainer"></div>
  19. <!-- 地址解析结果 -->
  20. <div class="address-result" v-if="address">
  21. <!-- 解析到地址:{{ address }}-->
  22. <el-form-item label="经度" class="input-item">
  23. <el-input v-model="lng" />
  24. </el-form-item>
  25. <el-form-item label="纬度" class="input-item">
  26. <el-input v-model="lat" />
  27. </el-form-item>
  28. <el-form-item label="详细地址" class="input-item">
  29. <el-input v-model="address" />
  30. </el-form-item>
  31. <el-button @click="confirmAddress" style="margin-left: 10px;" type="success">确认</el-button>
  32. </div>
  33. </div>
  34. </el-dialog>
  35. </div>
  36. </template>
  37. <script lang="ts" setup>
  38. import { defineEmits, defineExpose, nextTick, ref } from 'vue';
  39. import { Search } from '@element-plus/icons-vue';
  40. const mapContainer = ref<HTMLElement | null>(null);
  41. const address = ref('');
  42. const lng = ref('');
  43. const lat = ref('');
  44. const oldAddress = ref('');
  45. const searchKeyword = ref(''); // 搜索输入框的值
  46. const isShowDialog = ref(false);
  47. const marker = ref<BMapGL.Marker | null>(null);
  48. let map: BMapGL.Map | null = null;
  49. const openDialog = (row: any) => {
  50. oldAddress.value = '';
  51. isShowDialog.value = true;
  52. nextTick(() => {
  53. map = new BMapGL.Map(mapContainer.value!);
  54. // 如果添加了经纬度则进入地图后还原上次地址
  55. if (row.lng && row.lat) {
  56. lng.value = row.lng;
  57. lat.value = row.lat;
  58. searchByCoordinate();
  59. } else {
  60. const mapLngAndLat = JSON.parse(localStorage.sysinfo || '{"mapLngAndLat": null}').mapLngAndLat
  61. if (mapLngAndLat) {
  62. const [lng, lat] = mapLngAndLat.split(',')
  63. map.centerAndZoom(new BMapGL.Point(lng.trim(), lat.trim()), 10)
  64. } else {
  65. map.centerAndZoom('北京', 10);
  66. }
  67. }
  68. map.enableScrollWheelZoom(true);
  69. const scaleCtrl = new BMapGL.ScaleControl(); // 添加比例尺控件
  70. map.addControl(scaleCtrl);
  71. const zoomCtrl = new BMapGL.ZoomControl(); // 添加缩放控件
  72. map.addControl(zoomCtrl);
  73. map.addEventListener('click', (e: any) => {
  74. lng.value = e.latlng.lng.toFixed(5);
  75. lat.value = e.latlng.lat.toFixed(5);
  76. setMarker(e.latlng.lng.toFixed(5), e.latlng.lat.toFixed(5));
  77. setAddressByCoordinate(e.latlng.lng.toFixed(5), e.latlng.lat.toFixed(5));
  78. return false; // 阻止事件的冒泡和默认行为
  79. });
  80. map.addEventListener('zoomend', () => {
  81. const point = new BMapGL.Point(lng.value, lat.value);
  82. const marker = new BMapGL.Marker(point);
  83. // 获取标记位置
  84. const position = marker.getPosition();
  85. map?.setCenter(position); // 将标点设置为地图中心
  86. });
  87. if (row.address) {
  88. oldAddress.value = row.address;
  89. }
  90. });
  91. lng.value = "";
  92. lat.value = "";
  93. searchKeyword.value = "";
  94. };
  95. const confirmAddress = () => {
  96. isShowDialog.value = false;
  97. emit('updateMap', { lng: lng.value, lat: lat.value, address: address.value });
  98. };
  99. const setMarker = (lng: string, lat: string) => {
  100. // 删除之前的标记
  101. if (marker.value) {
  102. map?.removeOverlay(marker.value);
  103. }
  104. // 创建新的标记
  105. const point = new BMapGL.Point(lng, lat);
  106. marker.value = new BMapGL.Marker(point);
  107. map?.addOverlay(new BMapGL.Marker(point));
  108. // 移动地图中心到选点位置
  109. map?.setCenter(point);
  110. map?.centerAndZoom(point, 10);
  111. };
  112. const setAddressByCoordinate = (lng: string | number, lat: string | number) => {
  113. // 新查询经纬度方法
  114. map?.centerAndZoom(new BMapGL.Point(lng, lat), 18);
  115. // 创建地理编码实例, 并配置参数获取乡镇级数据
  116. const myGeo = new BMapGL.Geocoder({ extensions_town: true });
  117. // 根据坐标得到地址描述
  118. myGeo.getLocation(new BMapGL.Point(lng, lat), function (result) {
  119. if (result) {
  120. address.value = result.content.poi_desc;
  121. if (oldAddress.value) {
  122. address.value = oldAddress.value;
  123. }
  124. }
  125. });
  126. // TODO旧查询经纬度方法
  127. // const point = new BMapGL.Point(lng, lat);
  128. // const geocoder = new BMapGL.Geocoder();
  129. // geocoder.getPoint(point, (pointResult: any) => {
  130. // if (pointResult) {
  131. // const locationPoint = new BMapGL.Point(pointResult.lng, pointResult.lat);
  132. // geocoder.getLocation(locationPoint, (result: any) => {
  133. // if (result) {
  134. // // console.log(result);
  135. // // const formattedAddress = result.address;
  136. // // address.value = formattedAddress;
  137. // if (oldAddress.value) {
  138. // address.value = oldAddress.value;
  139. // }
  140. // }
  141. // });
  142. // }
  143. // });
  144. };
  145. const searchByCoordinate = () => {
  146. if (lng.value && lat.value) {
  147. setMarker(lng.value, lat.value);
  148. setAddressByCoordinate(lng.value, lat.value);
  149. } else if (searchKeyword.value) {
  150. lng.value = "";
  151. lat.value = "";
  152. searchByKeyword(searchKeyword.value);
  153. }
  154. };
  155. const searchByKeyword = (keyword?: string) => {
  156. if (keyword) {
  157. const localSearch = new BMapGL.LocalSearch(map);
  158. localSearch.setSearchCompleteCallback((searchResult) => {
  159. if (searchResult) {
  160. const poi = searchResult.getPoi(0);
  161. if (poi) {
  162. lng.value = poi.point.lng.toFixed(5);
  163. lat.value = poi.point.lat.toFixed(5);
  164. setMarker(poi.point.lng.toFixed(5), poi.point.lat.toFixed(5));
  165. setAddressByCoordinate(poi.point.lng.toFixed(5), poi.point.lat.toFixed(5));
  166. }
  167. }
  168. });
  169. address.value = keyword;
  170. localSearch.search(keyword ? keyword : searchKeyword.value);
  171. }
  172. };
  173. const emit = defineEmits(['updateMap']);
  174. defineExpose({ openDialog });
  175. </script>
  176. <style scoped>
  177. .map-container {
  178. display: flex;
  179. flex-direction: column;
  180. align-items: center;
  181. }
  182. .map {
  183. width: 100%;
  184. height: 50vh;
  185. }
  186. .coordinate-search {
  187. display: flex;
  188. margin-bottom: 20px;
  189. }
  190. .coordinate-search>div {
  191. margin-right: 10px;
  192. }
  193. .coordinate-search input {
  194. margin-right: 10px;
  195. width: 100px;
  196. }
  197. .input-item {
  198. margin-right: 10px;
  199. }
  200. .address-result {
  201. display: flex;
  202. flex-direction: row;
  203. margin-top: 20px;
  204. }
  205. </style>