【问题标题】:Google geocode. Parsing JSON issue谷歌地理编码。解析 JSON 问题
【发布时间】:2012-10-05 22:08:19
【问题描述】:

我想从他们的邮政/邮政编码(加拿大/美国)的谷歌地理编码中获取用户的城市、州/省、国家/地区。我以 JSON 格式检索数据:https://developers.google.com/maps/documentation/geocoding/#JSON

问题在于“address_components”的数量/顺序并不总是相同。我正在使用就像:$geocodedinfo['results'][0]['address_components'][3]['short_name']; 但我很快意识到这并不总是省/州,因为有时谷歌会在“address_components”下添加一个额外的元素。

有没有办法仅针对我需要的细节(即:城市、州/省、国家/地区)解析结果?

编辑当我得到它时,我实际上解码了 JSON json_decode($result, true)

Array
(
[results] => Array
    (
        [0] => Array
            (
                [address_components] => Array
                    (
                        [0] => Array
                            (
                                [long_name] => V2X 2P6
                                [short_name] => V2X 2P6
                                [types] => Array
                                    (
                                        [0] => postal_code
                                    )

                            )

                        [1] => Array
                            (
                                [long_name] => Maple Ridge
                                [short_name] => Maple Ridge
                                [types] => Array
                                    (
                                        [0] => locality
                                        [1] => political
                                    )

                            )

                        [2] => Array
                            (
                                [long_name] => Maple Ridge
                                [short_name] => Maple Ridge
                                [types] => Array
                                    (
                                        [0] => administrative_area_level_3
                                        [1] => political
                                    )

                            )

                        [3] => Array
                            (
                                [long_name] => Greater Vancouver Regional District
                                [short_name] => Greater Vancouver Regional District
                                [types] => Array
                                    (
                                        [0] => administrative_area_level_2
                                        [1] => political
                                    )

                            )

                        [4] => Array
                            (
                                [long_name] => British Columbia
                                [short_name] => BC
                                [types] => Array
                                    (
                                        [0] => administrative_area_level_1
                                        [1] => political
                                    )

                            )

                        [5] => Array
                            (
                                [long_name] => Canada
                                [short_name] => CA
                                [types] => Array
                                    (
                                        [0] => country
                                        [1] => political
                                    )

                            )

                    )

                [formatted_address] => Maple Ridge, BC V2X 2P6, Canada
                [geometry] => Array
                    (
                        [bounds] => Array
                            (
                                [northeast] => Array
                                    (
                                        [lat] => 49.2214351
                                        [lng] => -122.6577849
                                    )

                                [southwest] => Array
                                    (
                                        [lat] => 49.219268
                                        [lng] => -122.663613
                                    )

                            )

                        [location] => Array
                            (
                                [lat] => 49.2202679
                                [lng] => -122.660587
                            )

                        [location_type] => APPROXIMATE
                        [viewport] => Array
                            (
                                [northeast] => Array
                                    (
                                        [lat] => 49.2217005303
                                        [lng] => -122.6577849
                                    )

                                [southwest] => Array
                                    (
                                        [lat] => 49.2190025697
                                        [lng] => -122.663613
                                    )

                            )

                    )

                [types] => Array
                    (
                        [0] => postal_code
                    )

            )

    )

[status] => OK
)

【问题讨论】:

  • 我使用 XML 而不是 JSON,但我必须遍历直到找到包含所需数据的那个。
  • 我遇到了同样的问题,但在我的情况下,我需要大部分 address_components 所以我使用了 foreach() 来获得我想要的东西,而忘记了其余的。
  • @ayeshk 是否有可能遍历数组并仅获取 ['long_name'] 值 WHERE ['types']['0'] == 'country' 是不是?我以前从未这样做过。

标签: php json geocode


【解决方案1】:

您将不得不遍历结果。
大多数相关的 PHP 函数不支持多维数组,而那些支持的函数无论如何都会循环。

你可能会想要这样的东西:

foreach ($geocodedinfo["results"] as $result) {
    foreach ($result["address_components"] as $address) {
        // Repeat the following for each desired type
        if (in_array("country", $address["types"])) {
            $country = $address["long_name"];
        }
    }
}

【讨论】:

  • 这正是我所需要的。现在我知道了 in_array 函数。谢谢。
【解决方案2】:

您可以通过foreach 循环运行结果并获取您正在寻找的数据。

我正在对 Google 类型的含义进行最佳猜测,我可能不理解这些。

$translate = array(
    'locality' => 'city',
    'administrative_area_level_3' => 'area', // not sure what this one is
    'administrative_area_level_2' => 'county', // regional district ?
    'administrative_area_level_1' => 'state', // province
    'country' => 'country',
    'postal_code' => 'zip', // postal code
);

$address_components = array( );
foreach ($geocodedinfo['results'][0]['address_components'] as $component) {
    $address_components[$translate[$component['type'][0]]] = $component['long_name'];
}

var_dump($address_components);

【讨论】:

    【解决方案3】:

    试试看这个,http://php.net/manual/en/control-structures.foreach.php 它用于循环数组,你可以做类似的事情,

    foreach ($result['results'][0]['address_components'] as $key => $val) 
        { 
        if (in_array('country', $result['results'][0]['address_components'][$key]['types'][0])) 
        { 
        echo $result['results'][0]['address_components'][$key]['long_name']; 
        } 
    }
    

    类似的东西应该可以工作

    【讨论】:

    • 这不会捕获types 中的值顺序不同的结果,即["political", "country"] - 您需要使用in_array
    • 编辑了我的答案以解决这个问题。
    【解决方案4】:

    //解析json响应 $jsondata = json_decode($data,true); $results = $jsondata['results'][0]; $addr =$results['formatted_address'];

    这允许您访问长格式的地址

    326 London Road, Newbury, Berkshire, West Berkshire RG14 2DA, UK

    【讨论】:

      猜你喜欢
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-22
      相关资源
      最近更新 更多