1.去高德官网注册获取key和密钥
2.导入
import AMapLoader from '@amap/amap-jsapi-loader';
window._AMapSecurityConfig = {
securityJsCode: '',
};
3.vue代码
queryLocation() {
AMapLoader.load({
key: '',
version: '2.0',
plugins: [, 'AMap.Geolocation', 'AMap.AutoComplete'],
resizeEnable: true,
}).then((AMap) => {
const that = this;
that.getCurrentLocation(); //获取当前定位
});
},
getCurrentLocation() {
const that = this;
that.geolocation = new AMap.Geolocation({
timeout: 10000,
enableHighAccuracy: true,
});
that.geolocation.getCurrentPosition(function (status, result) {
if (status == 'complete') {
that.onComplete(result);
}
});
},
onComplete(data) {
let lat = data.position.lat;
let lng = data.position.lng;
// let result = wgs84_to_gcj02(lng, lat);
this.map.panTo([lat, lng]);
let marker = L.animatedMarker(
{ lat: lat, lng: lng },
{ iconDefault: this.getDefaultVehicle('location', this.factor) }
);
marker.addTo(this.map);
},