status": 240, "message": "APP 服務被禁用"
先使用百度API獲取IP經緯度,再使用反編譯獲取經緯度的地理信息
經查看是百度更新了接口,老的接口
URL = "http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=" + that.jingdu + "," + that.weidu + "&output=json&pois=1&latest_admin=1&ak=HmWt3r5SNn6bi6MBI4g5B03ad0pmRahX";
新的接口
'http://api.map.baidu.com/reverse_geocoding/v3/?ak=HmWt3r5SNn6bi6MBI4g5B03ad0pmRahX&output=json&coordtype=wgs84ll&location=' + latlon,
此處特別注意地理位置和參數有部分不同。
先貼正確代碼
var latlon = null;
//ajax獲取用戶所在經緯度
$.ajax({
url: "http://api.map.baidu.com/location/ip?ak=HmWt3r5SNn6bi6MBI4g5B03ad0pmRahX&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/reverse_geocoding/v3/?ak=HmWt3r5SNn6bi6MBI4g5B03ad0pmRahX&output=json&coordtype=wgs84ll&location=' + latlon,
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;
}
}
});
}
});