Parsing Google Geocoding JSON with PHP [closed] Parsing Google Geocoding JSON with PHP [closed] arrays arrays

Parsing Google Geocoding JSON with PHP [closed]


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

It helps if you spell it correctly ;-)


...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.