使用 Google 的地理编码 API 从 JSON 数组中获取正确的值

Get the right values from a JSON array using Google's Geocoding API

我正在使用 Google Geocoding APIPHP (cURL) 来获取 JSON 数组。 这一切都很好。 问题是并非每个输出都是相同的。

示例:

我有两个地址:

  1. 1 号大坝,阿姆斯特丹
  2. 柏林选帝侯大街 1 号

对于阿姆斯特丹地址,$array['results'][0]['address_components'] 的计数是 8,柏林地址的计数是 7。

JSON 输出:

阿姆斯特丹:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1",
               "short_name" : "1",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Dam",
               "short_name" : "Dam",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Centrum",
               "short_name" : "Centrum",
               "types" : [ "sublocality_level_1", "sublocality", "political" ]
            },
            {
               "long_name" : "Amsterdam",
               "short_name" : "Amsterdam",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Amsterdam",
               "short_name" : "Amsterdam",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Noord-Holland",
               "short_name" : "NH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Nederland",
               "short_name" : "NL",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "1012 JS",
               "short_name" : "1012 JS",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Dam 1, 1012 JS Amsterdam, Nederland",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 52.3740904,
                  "lng" : 4.8951464
               },
               "southwest" : {
                  "lat" : 52.37303319999999,
                  "lng" : 4.893589
               }
            },
            "location" : {
               "lat" : 52.3735618,
               "lng" : 4.8943677
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 52.37491078029149,
                  "lng" : 4.895716680291502
               },
               "southwest" : {
                  "lat" : 52.3722128197085,
                  "lng" : 4.893018719708498
               }
            }
         },
         "place_id" : "ChIJ46bYaccJxkcRR1k-7Pl25Kg",
         "types" : [ "premise" ]
      }
   ],
   "status" : "OK"
}

柏林:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1",
               "short_name" : "1",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Kurfürstendamm",
               "short_name" : "Kurfürstendamm",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Bezirk Charlottenburg-Wilmersdorf",
               "short_name" : "Bezirk Charlottenburg-Wilmersdorf",
               "types" : [ "sublocality_level_1", "sublocality", "political" ]
            },
            {
               "long_name" : "Berlin",
               "short_name" : "Berlin",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Berlin",
               "short_name" : "Berlin",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Duitsland",
               "short_name" : "DE",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "10719",
               "short_name" : "10719",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Kurfürstendamm 1, 10719 Berlin, Duitsland",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 52.50440769999999,
                  "lng" : 13.3337799
               },
               "southwest" : {
                  "lat" : 52.5043971,
                  "lng" : 13.3337737
               }
            },
            "location" : {
               "lat" : 52.50440769999999,
               "lng" : 13.3337737
            },
            "location_type" : "RANGE_INTERPOLATED",
            "viewport" : {
               "northeast" : {
                  "lat" : 52.50575138029149,
                  "lng" : 13.3351257802915
               },
               "southwest" : {
                  "lat" : 52.5030534197085,
                  "lng" : 13.3324278197085
               }
            }
         },
         "partial_match" : true,
         "place_id" : "EixLdXJmw7xyc3RlbmRhbW0gMSwgMTA3MTkgQmVybGluLCBEZXV0c2NobGFuZA",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

如您所见,国家和邮政编码索引不同(国家:6 和 5,邮政编码 7 和 6)

当我想像这样显示两个城市的街道、门牌号、邮政编码、城市和国家/地区时:

echo 'Street = '.$array['results'][0]['address_components'][1]['long_name'].'<br />';
echo 'Housenumber = '.$array['results'][0]['address_components'][0]['long_name'].'<br />';
echo 'Postalcode = '.$array['results'][0]['address_components'][7]['long_name'].'<br />';
echo 'City = '.$array['results'][0]['address_components'][3]['long_name'].'<br />';
echo 'Country = '.$array['results'][0]['address_components'][6]['long_name'].'<br />';

显示如下输出:

阿姆斯特丹:

Street = Dam
Housenumber = 1
Postalcode = 1012 JS
City = Amsterdam
Country = Netherlands

柏林:

Street = Kurfürstendamm
Housenumber = 1

Notice: Undefined offset: 7 in .... on line ....
Postalcode = 
City = Berlin
Country = 10719

所以问题是,如何在 "address_components" 数组中获取每个 "types""long_name"

或者也许有更好的方法?

您可以尝试使用下面的代码将 JSON 响应转换为以地址组件为键的数组:

function convertAddress($json)
{

    $data = json_decode($json);

    $address = array();

    foreach($data['results']['address_components'] AS $_component){

        $key = array_values($_component['types']);
        $address[$key[0]] = $_component;

    }

    return $address;

}

这对我来说非常适合你的测试数据,然后你可以像这样访问数据:

$address = convertAddress($berlin_address);
print $address['post_code']['long_name'];

请注意,我只处理了 JSON 的 address_components 部分,但其余部分应该很容易融入该方法。