acdr-ui/dist/dev/mp-weixin/utils/map-utils.js.map

1 line
7.6 KiB
Plaintext

{"version":3,"file":"map-utils.js","sources":["../../../../src/utils/map-utils.js"],"sourcesContent":["import config from './config' // 确保引入配置文件\r\n\r\n// H5 平台导入\r\n// #ifdef H5\r\nimport AMapLoader from '@amap/amap-jsapi-loader'\r\n\r\nlet AMap = null\r\nlet map = null\r\n\r\n// 初始化高德地图 (H5)\r\nconst initAMap = async () => {\r\n if (!AMap) {\r\n AMap = await AMapLoader.load({\r\n key: config.AMapKey,\r\n version: '2.0',\r\n plugins: ['AMap.Geocoder', 'AMap.Geolocation', 'AMap.ToolBar'],\r\n })\r\n }\r\n}\r\n\r\n// 初始化地图容器 (H5)\r\nexport const initMapH5 = async (containerId, center = [116.397428, 39.90923], zoom = 15) => {\r\n await initAMap() // 确保 AMap 已经加载\r\n\r\n if (!map) {\r\n map = new AMap.Map(containerId, {\r\n center,\r\n zoom,\r\n })\r\n }\r\n\r\n return map\r\n}\r\n\r\n// 获取当前位置和详细地址 (H5)\r\nexport const getCurrentLocationH5 = async () => {\r\n await initAMap() // 确保 AMap 已经加载\r\n\r\n return new Promise((resolve, reject) => {\r\n const geolocation = new AMap.Geolocation({\r\n enableHighAccuracy: true,\r\n timeout: 10000,\r\n zoomToAccuracy: true,\r\n })\r\n\r\n geolocation.getCurrentPosition((status, result) => {\r\n if (status === 'complete' && result) {\r\n const geocoder = new AMap.Geocoder()\r\n geocoder.getAddress([result.position.lng, result.position.lat], (geoStatus, geoResult) => {\r\n if (geoStatus === 'complete' && geoResult) {\r\n const locationInfo = {\r\n latitude: result.position.lat,\r\n longitude: result.position.lng,\r\n address: geoResult.regeocode.formattedAddress,\r\n }\r\n resolve(locationInfo)\r\n } else {\r\n reject('逆地理编码失败')\r\n }\r\n })\r\n } else {\r\n reject('定位失败,请检查网络或定位权限')\r\n }\r\n })\r\n })\r\n}\r\n\r\n// 显示当前位置在地图上 (H5)\r\nexport const showLocationOnMapH5 = (locationInfo) => {\r\n if (!map) return\r\n\r\n const marker = new AMap.Marker({\r\n position: [locationInfo.longitude, locationInfo.latitude],\r\n map,\r\n })\r\n\r\n map.setCenter([locationInfo.longitude, locationInfo.latitude])\r\n map.setFitView([marker]) // 自动调整地图视野以适应所有标记\r\n}\r\n\r\n// 添加两点之间的连线和显示距离 (H5)\r\nexport const addLineAndDistanceH5 = (startLocation, endLocation) => {\r\n if (!map) return\r\n\r\n const start = new AMap.LngLat(startLocation.longitude, startLocation.latitude)\r\n const end = new AMap.LngLat(endLocation.longitude, endLocation.latitude)\r\n\r\n const polyline = new AMap.Polyline({\r\n path: [start, end],\r\n isOutline: true,\r\n outlineColor: '#ffeeff',\r\n borderWeight: 2,\r\n strokeColor: '#3366FF',\r\n strokeOpacity: 1,\r\n strokeWeight: 3,\r\n strokeStyle: 'solid',\r\n strokeDasharray: [10, 5],\r\n })\r\n\r\n map.add(polyline)\r\n\r\n // 计算距离并在地图上显示\r\n const distance = Math.round(start.distance(end)) // 计算距离\r\n const distanceMarker = new AMap.Text({\r\n text: `${distance} 米`,\r\n anchor: 'bottom-center',\r\n position: new AMap.LngLat(\r\n (startLocation.longitude + endLocation.longitude) / 2,\r\n (startLocation.latitude + endLocation.latitude) / 2,\r\n ),\r\n })\r\n\r\n map.add(distanceMarker)\r\n\r\n map.setFitView([polyline]) // 调整地图视野使其适应连线\r\n}\r\n// #endif\r\n\r\n// 微信小程序平台导入\r\n// #ifdef MP-WEIXIN\r\nconst amapFile = require('../static/js/amap-wx')\r\n\r\nconst amapPlugin = new amapFile.AMapWX({\r\n key: config.AMapKey,\r\n})\r\n\r\n// 获取当前位置和详细地址 (微信小程序)\r\nexport const getCurrentLocationWx = () => {\r\n return new Promise((resolve, reject) => {\r\n amapPlugin.getRegeo({\r\n success: (data) => {\r\n if (data && data.length > 0) {\r\n const locationInfo = {\r\n latitude: data[0].latitude,\r\n longitude: data[0].longitude,\r\n address: data[0].regeocodeData.formatted_address,\r\n }\r\n resolve(locationInfo)\r\n } else {\r\n reject('未能获取到详细地址信息')\r\n }\r\n },\r\n fail: (error) => {\r\n reject('定位失败: ' + error.message)\r\n },\r\n })\r\n })\r\n}\r\n\r\n// 显示当前位置在地图上 (微信小程序)\r\nexport const showLocationOnMapWx = (mapContext, locationInfo) => {\r\n if (mapContext && locationInfo && locationInfo.latitude && locationInfo.longitude) {\r\n mapContext.moveToLocation({\r\n latitude: locationInfo.latitude,\r\n longitude: locationInfo.longitude,\r\n })\r\n } else {\r\n console.error('Invalid mapContext or locationInfo')\r\n }\r\n}\r\n\r\n// 添加两点之间的连线和显示距离 (微信小程序)\r\nexport const addLineAndDistanceWx = (mapContext, startLocation, endLocation) => {\r\n mapContext.addPolyline({\r\n points: [\r\n {\r\n longitude: startLocation.longitude,\r\n latitude: startLocation.latitude,\r\n },\r\n {\r\n longitude: endLocation.longitude,\r\n latitude: endLocation.latitude,\r\n },\r\n ],\r\n color: '#3366FF',\r\n width: 4,\r\n dottedLine: false,\r\n })\r\n\r\n amapPlugin.getDistance({\r\n origin: `${startLocation.longitude},${startLocation.latitude}`,\r\n destination: `${endLocation.longitude},${endLocation.latitude}`,\r\n success: (data) => {\r\n if (data && data.distance) {\r\n // 显示距离(在页面中处理,微信地图不直接支持文本标记)\r\n console.log(`距离: ${data.distance} 米`)\r\n }\r\n },\r\n })\r\n}\r\n\r\n// 微信小程序的初始化地图函数\r\nconst initMapWeixin = (containerId, center, zoom) => {\r\n const mapContext = wx.createMapContext(containerId)\r\n\r\n // 设置中心点和缩放级别(微信小程序中缩放级别可以在地图组件中设置)\r\n mapContext.moveToLocation({\r\n latitude: center[1],\r\n longitude: center[0],\r\n })\r\n\r\n // 可以返回 mapContext 以便后续使用\r\n return mapContext\r\n}\r\n// #endif\r\n\r\n// 公共API\r\n\r\n// 初始化地图 (根据平台调用)\r\nexport const initMap = async (containerId, center = [116.397428, 39.90923], zoom = 15) => {\r\n // #ifdef H5\r\n return await initMapH5(containerId, center, zoom)\r\n // #endif\r\n\r\n // #ifdef MP-WEIXIN\r\n return initMapWeixin(containerId, center, zoom)\r\n // #endif\r\n}\r\n\r\n// 获取当前位置和详细地址\r\nexport const getCurrentLocation = () => {\r\n // #ifdef H5\r\n return getCurrentLocationH5()\r\n // #endif\r\n\r\n // #ifdef MP-WEIXIN\r\n return getCurrentLocationWx()\r\n // #endif\r\n}\r\n\r\n// 显示当前位置在地图上\r\nexport const showLocationOnMap = (mapContextOrContainerId, locationInfo) => {\r\n // #ifdef H5\r\n showLocationOnMapH5(locationInfo)\r\n // #endif\r\n\r\n // #ifdef MP-WEIXIN\r\n showLocationOnMapWx(mapContextOrContainerId, locationInfo)\r\n // #endif\r\n}\r\n\r\n// 添加两点之间的连线和显示距离\r\nexport const addLineAndDistance = (mapContextOrContainerId, startLocation, endLocation) => {\r\n // #ifdef H5\r\n addLineAndDistanceH5(startLocation, endLocation)\r\n // #endif\r\n\r\n // #ifdef MP-WEIXIN\r\n addLineAndDistanceWx(mapContextOrContainerId, startLocation, endLocation)\r\n // #endif\r\n}\r\n"],"names":["config"],"mappings":";;;AAwHA,MAAM,WAAW,QAAQ,sBAAsB;AAE5B,IAAI,SAAS,OAAO;AAAA,EACrC,KAAKA,aAAM,OAAC;AACd,CAAC;"}