我使用此代碼來獲得用戶的詳細地址信息用戶城市和國家信息獲得使用谷歌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