JK87
JK87

Reputation: 379

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

I'm using the Google Geocoding API, and PHP (cURL) to get the JSON array. That's all working fine. The problem is that not every output is the same.

Example:

I have two addresses:

  1. Dam 1, Amsterdam
  2. Kurfürstendamm 1, Berlin

For the Amsterdam address the count of $array['results'][0]['address_components'] is 8 and the one for Berlin 7.

JSON output:

Amsterdam:

{
   "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"
}

Berlin:

{
   "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"
}

As you can see the country and postalcode indexes are different (country: 6 and 5, postalcode 7 and 6)

When I want to show the street, housenumber, postalcode, city and country for both cities something like this:

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 />';

It shows the following output:

Amsterdam:

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

Berlin:

Street = Kurfürstendamm
Housenumber = 1

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

So the question is, how can I get the "long_name" per "types" in the "address_components" array?

Or maybe there are even better ways?

Upvotes: 0

Views: 729

Answers (1)

codeguy
codeguy

Reputation: 700

You could try the following code below to convert the JSON reponse to an array with the address components as keys:

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;

}

This works great for me with your test data, you can then access data like so:

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

Please note I have only worked on the address_components part of the JSON, the rest should be easy to work into the method though.

Upvotes: 2

Related Questions