使用百度地图js api定位并获得精确地址:
var map = new BMap.Map("map");
var geolocation = new BMap.Geolocation();
//开启SDK辅助定位,仅当使用环境为移动web混合开发,且开启了定位sdk辅助定位功能后生效
geolocation.enableSDKLocation();
//调用getCurrentPosition函数
geolocation.getCurrentPosition(function(position) {
point = position.point;
console.log(point);
$.ajax({
url:"https://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location="+point.lat+","+point.lng+"&output=json&pois=1&ak=你的key",
type:"POST",
dataType:"jsonp",
success:function (res) {
var address = res.result.formatted_address+'('+res.result.pois[0].addr+res.result.pois[0].name+')';
alert(address);
},
error:function (e) {
console.log(e);
alert("错误");
}
});
});
注意:使用前需前往百度地图官网申请一个key,申请入口:http://lbsyun.baidu.com/apiconsole/key