Need Google Geocoding to return City name in English

冷暖自知 提交于 2019-12-07 23:03:36

问题


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&region=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:

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!!


回答1:


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).



来源:https://stackoverflow.com/questions/3438741/need-google-geocoding-to-return-city-name-in-english

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!