How to get formatted address from reverse geocoding JSON result? How to get formatted address from reverse geocoding JSON result? json json

How to get formatted address from reverse geocoding JSON result?


The following example demonstrates how to parse and access Geocoding Service result long_name & short_name properties in PHP:

$json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=2.993518,101.7874058&sensor=true');$data = json_decode($json, true);foreach ($data['results'] as $item) {    if( !empty( $item['address_components'] ) ){        $longName = $item['address_components'][0]['long_name'];         $shortName = $item['address_components'][0]['short_name'];         print_r('Long Name:' . $longName);        print_r(' Short Name:' . $shortName);    }}

PhpFiddle


First, you can read all about the geocoder result types here -> https://developers.google.com/maps/documentation/geocoding/intro

I would convert the JSON to a stdClass for convenience :

$location = json_decode($location);

Then parse it like this :

$geoResults = [];foreach($location->results as $result){    $geoResult = [];        foreach ($result->address_components as $address) {        if ($address->types[0] == 'country') {            $geoResult['country'] = $address->long_name;        }        if ($address->types[0] == 'administrative_area_level_1') {            $geoResult['state'] = $address->long_name;        }        if ($address->types[0] == 'administrative_area_level_2') {            $geoResult['county'] = $address->long_name;        }        if ($address->types[0] == 'locality') {            $geoResult['city'] = $address->long_name;        }        if ($address->types[0] == 'postal_code') {            $geoResult['postal_code'] = $address->long_name;        }               if ($address->types[0] == 'route') {            $geoResult['route'] = $address->long_name;        }           }    $geoResults[] = $geoResult;}

Now you have a $geoResults array with named properties :

Array(    [0] => Array        (            [route] => Jalan Sungai Chua            [city] => Kajang            [state] => Selangor            [country] => Malaysia            [postal_code] => 43000        )    [1] => Array        (            [city] => Kajang            [state] => Selangor            [country] => Malaysia            [postal_code] => 43000        )    [2] => Array        (            [city] => Kajang            [state] => Selangor            [country] => Malaysia            [postal_code] => 43000        )    [3] => Array        (            [city] => Kajang            [state] => Selangor            [country] => Malaysia        )    [4] => Array        (            [state] => Selangor            [country] => Malaysia        )    [5] => Array        (            [postal_code] => 43000            [state] => Selangor            [country] => Malaysia        )    [6] => Array        (            [state] => Selangor            [country] => Malaysia        )    [7] => Array        (            [state] => Selangor            [country] => Malaysia        )    [8] => Array        (            [country] => Malaysia        ))

You can iterate over all found cities like this :

foreach($geoResults as $result) {    echo isset($result['city']) ? $result['city'] : 'N/A';    echo '<br>';}