vue使用高德地图Api获取当前经纬度信息
在utils里面新建getLocation.js 封装获取经纬度的公用方法(优化加载速度动态cdn引入高德地图) 由于高德Api方法获取当前经纬度比较慢,如果需求是在获取到当前经纬度数据之后请求一些数据,需要搭配promise使用保证获取到经纬度信息。具体见下面代码
function loadSDK() {
if (window.AMap) return
return new Promise((resolve, reject) => {
const script = document.createElement('script')
script.src =
'http://webapi.amap.com/maps?v=1.4.6&key=*****************' //***为申请的高德key
document.head.appendChild(script)
script.onload = resolve
script.onerror = reject
})
}
export default async () => {
await loadSDK()
return new Promise((resolve) => {
// eslint-disable-next-line no-undef
AMap.plugin('