代码
var map = new BMap.Map('map');
map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
// 初始化控件
var locationControl = new BMap.LocationControl({
showAddressBar: true,
});
// 定位成功监听
locationControl.addEventListener('locationSuccess', function (e) {
var address = '';
address += e.addressComponent.province;
address += e.addressComponent.city;
address += e.addressComponent.district;
address += e.addressComponent.street;
address += e.addressComponent.streetNumber;
document.querySelector('#text').innerText = '当前定位地址为:' + address;
});
// 定位失败监听
locationControl.addEventListener('locationError', function (e) {
alert(e.message);
});
// 添加控件到地图上
map.addControl(locationControl);
预览