使用百度地图api实现网站定位功能
如题,废话不多说直接上代码:
//首先引入jqurey.js 方便ajax的请求// 百度地图API功能
var map = new BMap.Map("nowmap");
// 根据浏览器获取经纬度定位当前位置
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
var mk = new BMap.Marker(r.point);
//获取浏览器当前经纬度
var lat=r.point.lat;
var lng=r.point.lng;
//开始向百度发送请求
$.ajax({
url:'http://api.map.baidu.com/geocoder/v2/?ak=SjU291131hitQDaSlTUKSSsILGmMmpra&location='+lat+','+lng+'&output=json',
dataType:'jsonp',
beforeSend:function(){
document.getElementById('now-address').innerHTML='自动定位中...';
document.getElementById('diqu').innerHTML='定位中...';
},
//百度返回json后所执行的操作
success:function(datas){
document.getElementById('now-address').innerHTML=(datas['result']['addressComponent']['district']+datas['result']['addressComponent']['street']+datas['result']['addressComponent']['street_number']);
document.getElementById('diqu').innerHTML=datas['result']['addressComponent']['city'];
}
})
}
else {
document.getElementById('diqu').innerHTML='定位失败';
document.getElementById('now-address').innerHTML='定位失败,请重试';
// alert('failed'+this.getStatus());
}
},{enableHighAccuracy: true})最后附上 百度地图接口文档地址:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding#8..E8.BF.94.E5.9B.9E.E7.A0.81.E7.8A.B6.E6.80.81.E8.A1.A8
Dcr163的博客
http://dcr163.cn/88.html(转载时请注明本文出处及文章链接)