页面统一调用:
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=Xbq3g4meudxD5Q0MB9osTLpg"></script>
百度密钥:Xbq3g4meudxD5Q0MB9osTLpg
JS调用:
方法一:
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
mylng = r.point.lng; //经度
mylat = r.point.lat; //纬度
var url="http://api.map.baidu.com/geocoder/v2/?ak=Xbq3g4meudxD5Q0MB9osTLpg&callback=renderReverse&location="+mylat+","+mylng+"&output=json&pois=0";
var script=document.createElement('script');
script.type='text/javascript';
script.src=url;
document.body.appendChild(script);
}else{
alert('failed'+this.getStatus());
}
},{enableHighAccuracy: true});
renderReverse=function(response){
var addr=response.result.formatted_address;
alert('您现在的位置:'+addr);
}
方法二:
//获取当前地址
var geolocation = new BMap.Geolocation();
var geoc = new BMap.Geocoder();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
mylng = r.point.lng; //经度
mylat = r.point.lat; //纬度
var point = new BMap.Point(mylng,mylat);
geoc.getLocation(point, function(rs){
var addComp = rs.addressComponents;
alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
});
}else{
alert('failed'+this.getStatus());
}
},{enableHighAccuracy: true});