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

1 line
7.6 KiB
Plaintext
Raw Normal View History

2024-09-19 07:20:14 +08:00
{"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