Pregunta

I want to get a users city, state/province, country from a google geocode of their postal/zip code (canada/usa). I retrieve the data as JSON: https://developers.google.com/maps/documentation/geocoding/#JSON

The problem is that there is not always the same amount/order of 'address_components'. I was using just like: $geocodedinfo['results'][0]['address_components'][3]['short_name']; But I soon realized that that will not always be province/state because sometimes google will add an extra element under 'address_components'.

Is there a way to parse the results just for the specifics I need (ie: City, State/Province, Country)?

EDIT I actually decode the JSON when I get it 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
)
¿Fue útil?

Solución

You're going to have to loop over the results.
Most of the relevant PHP functions don't support multidimensional arrays, and the ones that do will be looping anyway.

You'll probably want something like this:

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"];
        }
    }
}

Otros consejos

You can run the results through a foreach loop and get the data out you are looking for.

I'm taking a best guess at the meanings of the Google types, I may be off on those.

$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);

try looking at this, http://php.net/manual/en/control-structures.foreach.php it's for looping over arrays, you can do something like,

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']; 
    } 
}

Something like that should work

// parse the json response $jsondata = json_decode($data,true); $results = $jsondata['results'][0]; $addr =$results['formatted_address'];

This allows you to access the long formatted address

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top