//单独写一个js文件 export default function MapLoader() { return new Promise((resolve, reject) => { if (window.AMap) { resolve(window.AMap); } else { window._AMapSecurityConfig = { securityJsCode: ' 高德地图在 2021-12-12 更新之后,创建的key 会产生一个秘钥 ', } var script = document.createElement('script'); script.type = "text/javascript"; script.async = true; script.src = "https://webapi.amap.com/maps?v=1.4.15&key= 高德地图的key &callback=initAMap"; script.onerror = reject; document.head.appendChild(script); } window.initAMap = () => { resolve(window.AMap); }; }); }
//高德地图的应用地址 https://console.amap.com/dev/key/app
import AMap from "../../../utils/AMap.js" //这里引入上面那个JS的包
//放在 method 里的方法
async initAMap() { this.resAmap = await AMap(); this.$nextTick(function() { // this.getBroewerLatLng(); this.resAmap.plugin('AMap.Geolocation', () => { var geolocation = new this.resAmap.Geolocation({ enableHighAccuracy: true, //是否使用高精度定位,默认:true timeout: 10000, //超过10秒后停止定位,默认:5s noGeoLocation: 3, //是否禁止使用浏览器Geolocation定位,默认值为0,可选值0-3 0: 可以使用浏览器定位 1: 手机设备禁止使用浏览器定位 2: PC上禁止使用浏览器定位 3:有终端禁止使用浏览器定位 convert: false, //是否使用坐标偏移,取值true:为高德地图坐标,取值false:为浏览器定位坐标默认值:true // GeoLocationFirst: true, //默认为false,设置为true的时候可以调整PC端为优先使用浏览器定位,失败后使用IP定位 }); geolocation.getCurrentPosition(function(status, result) { if (status == 'complete') { // alert(result.position); result.position = result.position.toString() var positions = result.position.split(',') this.latitude = parseInt(positions[0]) this.longitude = parseInt(positions[1]) this.covers[0].latitude = parseInt(positions[1]) this.covers[0].longitude = parseInt(positions[0]) setTimeout(() => { this.getUserLocation(parseInt(positions[1]), parseInt( positions[0])); }, 1000) } else { this.initAMap() } }); }); }) },
//由于是同步锁,所以放在哪里加载并没有所谓。目前暂时没有解决同步锁的方法,后面如果有会同步更新