使用百度地圖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