1. Vue introduces Baidu map
npm install vue-baidu-map --save
Register globally in main.js, and import all the components of the Baidu map component library at one time.
import Vue from 'vue'
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
// ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
ak: 'YOUR_APP_KEY'
})
Get the current address method: getlocation() { let me = this new BMap.LocalCity().get(function(r) { // locate the city alert('Current location city:' + r.name) if (r.name !== '') { me.form.location = r.name.replace(/city/g, '').replace(/district/g, '').replace(/county/g, '') } }) },
Mainly the new BMap.LocalCity().get(function(r) method.
Get current location latitude and longitude, street information
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function (r) {
if (this.getStatus() == BMAP_STATUS_SUCCESS) {
var position = {
lng: r.point.lng,
lat: r.point.lat
}
console.log('---------', r)//打印r,从r中取所需信息
alert('您的位置:' + r.point.lng + ',' + r.point.lat);
}
})