1、獲取key
我們需要到[高德]開放平臺生成key

image.png
我這里選的是web端,提交后會生成一個key值

image.png

image.png
2、安裝vue-amap
npm install vue-amap --save
3、在項目main.js引入vue-amap
import AMap from 'vue-amap';
Vue.use(AMap);
// 初始化vue-amap
AMap.initAMapApiLoader({
// 高德key
key: '放入你的key',
// 插件集合 (插件按需引入)
plugin: ['AMap.Geolocation']
});
3.使用
<template>
<div class="box">
<div :style="{width:'100%',height:'300px'}">
<el-amap vid="amap" :plugin="plugin" class="amap-demo" :center="center"></el-amap>
</div>
</div>
</template>
<script>
export default {
data(){
const self = this;
return {
center: [121.59996, 31.197646],
lng: 0,
lat: 0,
loaded: false,
plugin: [{
enableHighAccuracy: true,//是否使用高精度定位,默認(rèn):true
timeout: 100, //超過10秒后停止定位,默認(rèn):無窮大
maximumAge: 0, //定位結(jié)果緩存0毫秒,默認(rèn):0
convert: true, //自動偏移坐標(biāo),偏移后的坐標(biāo)為高德坐標(biāo),默認(rèn):true
showButton: true, //顯示定位按鈕,默認(rèn):true
buttonPosition: 'RB', //定位按鈕停靠位置,默認(rèn):'LB',左下角
showMarker: true, //定位成功后在定位到的位置顯示點標(biāo)記,默認(rèn):true
showCircle: true, //定位成功后用圓圈表示定位精度范圍,默認(rèn):true
panToLocation: true, //定位成功后將定位到的位置作為地圖中心點,默認(rèn):true
zoomToAccuracy:true,//定位成功后調(diào)整地圖視野范圍使定位位置及精度范圍視野內(nèi)可見,默認(rèn):f
extensions:'all',
pName: 'Geolocation',
events: {
init(o) {
// o 是高德地圖定位插件實例
o.getCurrentPosition((status, result) => {
console.log(result)
if (result && result.position) {
self.lng = result.position.lng;
self.lat = result.position.lat;
self.center = [self.lng, self.lat];
self.loaded = true;
self.$nextTick();
}
});
}
}
}]
}
}
}
</script>