我使用此代码来获得用户的详细地址信息用户城市和国家信息获得使用谷歌GeoApi
function getGeo() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (a) {
$("#geoLoc").html("Determing your location..");
$.post("https://mysite.com/getloc.php?l=" + a.coords.latitude + "," + a.coords.longitude, function (b) {
var c = jsonParse(b);
geo_adres = c.results[0].formatted_address;
latitude = a.coords.latitude;
longitude = a.coords.longitude;
$("#geoLoc").html(c.results[0].formatted_address);
$("#geoLoc").show("slow")
})
}, function (a) {
alert(geolocationErrorMessages[a.code])
}, {
enableHighAccuracy: true,
maximumAge: 12e4
});
return false
} else {
alert("Your browser doesn't support geo-location feature...")
}
}
编辑: getloc.php包含此代码(在C写的JavaScript VAR)
$data = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng=". $_GET['l'] ."&sensor=false");
print $data;
其实我要的就是这样city, country
我应该如何改变这一c.results[0].formatted_address
到让用户城市和国家信息那么?
你在c中得到什么? – swapnesh
我编辑了我的问题,请检查 –
您应该查看geocoder api的文档,[这里](https://developers.google.com/maps/documentation/geocoding/#Results)。我将首先检查数组'c.results [0] .address_components []'的内容。 – Lee