引入js
<script src="https://api.map.baidu.com/api?v=2.0&ak={{秘钥}}"></script>
lbs.js
function getLocation(callback) {
var geol = new BMap.Geolocation();
var geoc = new BMap.Geocoder();
geol.getCurrentPosition(
rst => {
geoc.getLocation(rst.point, function (rs) {
callback({
...rs.point,
...rs.addressComponents,
});
});
},
err => {
var errmsg = '';
switch (err.code) {
case error.PERMISSION_DENIED:
errmsg = '用户拒绝';
break;
case error.POSITION_UNAVAILABLE:
errmsg = '无法获取';
break;
case error.TIMEOUT:
errmsg = '请求超时';
break;
case error.SERVICE_UNAVAILABLE:
errmsg = '服务不可用';
break;
case error.UNKNOWN_ERROR:
errmsg = '未知的错误';
break;
default:
errmsg = err;
break;
}
callback({
msg: errmsg,
code: -1,
});
},
{ enableHighAccuracy: true, timeout: 5000, maximumAge: 0 }
)
}
export default getLocation
callback 返回结构
{
lat: '',
lng: '',
of: 'inner',
province: '',
city: '',
district: '',
street: '',
streetNumber: '',
}
来源:oschina
链接:https://my.oschina.net/qiongtaoli/blog/4420305