需要Google地理编码以英语返回城市名称 [英] Need Google Geocoding to return City name in English
问题描述
我有以下代码可用于要求Google纠正输入的地址,并需要它返回城市的英文名称:
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);
}
然后
$xml = gmap_query_xml($in_address);
最后得到城市名称:
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 Киев.
如何解决?谢谢!
推荐答案
在这方面似乎有些混乱.请参阅此Google Geocoding API错误报告列表.
There seems to be a bit of a chaos in that regard. See this list of Google Geocoding API bug reports.
您正在使用旧的V2 API.
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
对language
参数更敏感,并且可以正确转换位置名称.我得到了language=en
(罗马),language=de
(罗马)甚至language=fi
(罗马)正确的结果!
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)!
但是,V3以不同的格式提供其地理编码结果,因此您必须大幅度地更改解析(由于addressComponents
的结构方式,这也使我感到头疼).
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).
这篇关于需要Google地理编码以英语返回城市名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!