1.引入 百度地图js(1.3以后需要key) <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script>
2.js如下:
var lat=32.4,lng=119.4,page=1;//默认扬州市经纬度
var areaName="扬州市";//定位获取城市 默认扬州市
getLocation();
function getLocation(){
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
lat = r.point.lat;
lng = r.point.lng;
var point = new BMap.Point(lng,lat);
var gc = new BMap.Geocoder();
gc.getLocation(point,function(rs){
var addComp = rs.addressComponents;
alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
});
}else{
alert("定位失败")
}
});
}