function getCity() {
var latlon = null;
//ajax获取用户所在经纬度
$.ajax({
url: "http://api.map.baidu.com/location/ip?ak=YourAK&coor=bd09ll",
type: "POST",
dataType: "jsonp",
success: function (data) {
latlon = data.content.point.y + "," + data.content.point.x;
//ajax根据经纬度获取省市区
$.ajax({
type: "POST",
dataType: "jsonp",
url: 'http://api.map.baidu.com/geocoder/v2/?ak=YourAK&callback=renderReverse&location=' + latlon + '&output=json&pois=0',
success: function (response) {
if (response.status == 0) {
console.log(response.result.addressComponent.province + "-" + response.result.addressComponent.city + "-" + response.result.addressComponent.district);
city = response.result.addressComponent.city;
}
}
});
}
});
}
百度地图JS获取当前所在城市信息
最新推荐文章于 2024-07-07 20:42:23 发布
本文介绍了一种使用百度地图API通过用户IP地址获取其经纬度信息,并进一步调用地理编码API解析出所在省份、城市及区县的具体实现方法。此方案适用于需要定位用户位置的Web应用。

1199

被折叠的 条评论
为什么被折叠?



