在vue项目中的index.html中添加如下代码(其中ak部分需要通过百度开放平台申请):
<script type="text/javascript" src="https://api.map.baidu.com/api?v=1.0&type=webgl&ak=你的密钥"></script>
在需要使用的页面当中应用(放在<script>标签中)
export default{
data(){
return {
position: '', //位置
}
},
mounted(){
var geolocation = new BMapGL.Geolocation();
var that=this;
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
const province = r.address.province //省
const city = r.address.city //市
const district = r.address.district //区
const locations = province + ' '+ city + ' ' + district //省 市 区
that.position = locations
}
})
},
}