Need Google Geocoding to return City name in English
I have this code for requesting google to correct the typed in address, and need it to return the English name for the city:
function gmap_query_xml($in_address) {
$base_url = "http://maps.google.com/maps/geo?output=xml®ion=US&language=en&key=". KEY;
$request_url = $base_url . "&q=" . urlencode($in_address);
return simplexml_load_file($request_url);
}
Then,
$xml = gmap_query_xml($in_address);
And finally to get the city name:
开发者_StackOverflow中文版if ($xml) {
$city = (string) $xml->Response->Placemark->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->LocalityName;
}
This returns the correct city name, BUT! it's represented in the native language. Try Rome - you get Roma, try Kiev and you will get Киев.
How this can be solved? Thank!!
There seems to be a bit of a chaos in that regard. See this list of Google Geocoding API bug reports.
You are using the old V2 API.
According to this post and my own tests, the new V3 API:
http://maps.google.com/maps/api/geocode/xml?address=Rome&sensor=false&language=en
is more sensitive to the language
parameter and translates locality names correctly. I get correct results for language=en
(Rome), language=de
(Rom) and even language=fi
(Rooma)!
However, V3 serves its geocoding results in a different format so you would have to change your parsing considerably (Due to the way addressComponents
is structured now, this bugged the heck out of me too).
精彩评论