0
返回几个值,我收到的全地址使用谷歌的地理编码API从JSON
http://maps.google.com/maps/api/geocode/json?address={$address}
JSON格式的输出。然后拉这样的经度和纬度
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
所有这些都很好。但我也需要被包含在这里
...
"results" : [
{
"address_components" : [
{
"long_name" : "Platte City",
"short_name" : "Platte City",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Carroll Township",
"short_name" : "Carroll Township",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Platte County",
"short_name" : "Platte County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Missouri",
"short_name" : "MO",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "64079",
"short_name" : "64079",
"types" : [ "postal_code" ]
}
],
...
我试图
for ($i = 0; $i < 10; $i++) {
$type2 = $resp['results'][0]['address_components'][$i]['types'];
if (stripos(strtolower($type2), 'administrative_area_level_2') !== false){
$county = $resp['results'][0]['address_components'][$i]['long_name'];
} else {$county = "$i Unknown County";}
}
正如你所看到的县是“administrative_area_level_2”,但尝试了几个小时后,我得到的是“未知县的县名”。 Google API声明它不会总是在XML/JSON中的相同位置,并且看起来是正确的。有时候它的位置3有时候是5,有时候是其他地方。 有人可以给我一个如何获得县名的例子。