百度地图获取地理位置

引入js

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=l0iWNQvpVVEb1UWxv2b1z2g5"></script>

获取地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function accurate_Location() {
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r) {
if (this.getStatus() == BMAP_STATUS_SUCCESS) {
//获取经纬度
console.log('坐标:'+r.point.lng+','+r.point.lat);
var point = new BMap.Point("" + r.point.lng + "", "" + r.point.lat + "");
//地址解析
var geoc = new BMap.Geocoder();
geoc.getLocation(point, function(rs) {
var addComp = rs.addressComponents;
console.log('位置:' + addComp.province + "-" + addComp.city + "-" + addComp.district + "-" + addComp.street + "-" + addComp.streetNumber);
});
} else if (this.getStatus() == BMAP_STATUS_UNKNOWN_LOCATION) {
console.log('位置结果未知!');
} else if (this.getStatus() == BMAP_STATUS_INVALID_REQUEST) {
console.log('非法请求!');
} else if (this.getStatus() == BMAP_STATUS_PERMISSION_DENIED) {
console.log('没有权限!');
} else if (this.getStatus() == BMAP_STATUS_SERVICE_UNAVAILABLE) {
console.log('服务不可用!');
} else if (this.getStatus() == BMAP_STATUS_TIMEOUT) {
console.log('请求超时!');
}
});
}
0%