Parsing Google Geocoding JSON with PHP

21,424

Solution 1

echo $json['results'][0]['formatted_address'];

It helps if you spell it correctly ;-)

Solution 2

...just FYI, the JSON request URL has to be properly formatted in order to return anything good otherwise you may get NULL response.

For example, this is how I retrieve longitude and latitude values from the JSON response:

// some address values
    $client_address = '123 street';
    $client_city = 'Some City';
    $client_state = 'Some State';
    $client_zip = 'postal code';

// building the JSON URL string for Google API call 
    $g_address = str_replace(' ', '+', trim($client_address)).",";
    $g_city    = '+'.str_replace(' ', '+', trim($client_city)).",";
    $g_state   = '+'.str_replace(' ', '+', trim($client_state));
    $g_zip     = isset($client_zip)? '+'.str_replace(' ', '', trim($client_zip)) : '';

$g_addr_str = $g_address.$g_city.$g_state.$g_zip;       
$url = "http://maps.google.com/maps/api/geocode/json?
        address=$g_addr_str&sensor=false";

// Parsing the JSON response from the Google Geocode API to get exact map coordinates:
// latitude , longitude (see the Google doc. for the complete data return here:
// https://developers.google.com/maps/documentation/geocoding/.)

$jsonData   = file_get_contents($url);

$data = json_decode($jsonData);

$xlat = $data->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$xlong = $data->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

echo $xlat.",".$xlong;

...also, you can use the same code to hard-code the Google Map iframe and embed it into you page if the v3 API does not behave... see simple tutorial here: http://pmcds.ca/blog/embedding-google-maps-into-the-webpage.html

Embedding the is not exactly the right approach but there are times when it becomes needed solution.

Share:
21,424
Lee Price
Author by

Lee Price

Updated on August 16, 2020

Comments

  • Lee Price
    Lee Price almost 4 years

    I'm trying to parse the json response from the Google Geocode API but I'm having a little trouble understanding it.

    For those unfamiliar with the Geocode API here is the URL: http://maps.google.com/maps/api/geocode/json?address=albert%20square&sensor=false

    I'm using the following code to parse the request

    <?php
    
    $address = urlencode($_POST['address']);
    $request = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=" . $address . "&sensor=false");
    $json = json_decode($request, true);
    
    ?>
    

    And I'm trying to output with the following:

    echo $json['results'][0]['formated_address'];
    

    I'm not sure why nothing is being echoed. I've also tried $json[0]['results'][0]['formated_address']. I know this is a noob question, but multi-dimensional arrays confuse me.

  • Lee Price
    Lee Price almost 12 years
    doh! I'm such an idiot sometimes
  • Bas van Stein
    Bas van Stein about 7 years
    I would use urlencode() instead of str_replace(), this takes care of the spaces and any other special characters for the address.