1

I have read a few Q&As on here and additional articles, but cannot get this script to display the relevant values from the JSON return (which are long and lat from this Google Maps call).

<?php

// Address for Google to search
$address = 'London,UK';

// Get the map json data from Google Maps using the $address variable
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';

$json = file_get_contents($googleCall);
//header("Content-type: application/json");
//echo $json;

echo $json->results->geometry->location->lat;
echo $json->results->geometry->location->lng;
?>   

I am sure I am 99% there, just cannot see where the error is.

1
  • 3
    json_decode is missing in your code. Commented Jun 9, 2013 at 14:46

3 Answers 3

2

You can decode your json as an associative array and the access to all data with scopes

$address = 'London,UK';
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';

$json = file_get_contents($googleCall);
$array = json_decode($json, true); //note second parameter on true as we need associative array

echo $array['results'][0]['geometry']['location']['lat'] . '<br>';
echo $array['results'][0]['geometry']['location']['lng'];

This will output

51.5112139
-0.1198244
Sign up to request clarification or add additional context in comments.

9 Comments

You can access all data without making it an associative array.
Out of interest, most articles didnt wrap the values in ''. Should it be either way or is this the 'right' way.
@PHP-L-Driver without quotes php will throw notice about using undefined constants
@PHP-L-Driver you are welcome dude. If you feel answer was correct and it helped you out please consider to accept it, so you will also help people in the future with similar problems. To accept you just need to click on the large green tick mark (✔) under the answer's score.
Just curious though, why the ,true? I know it makes it an associative array, but why?
|
0

try this out:

<?php

// Address for Google to search
$address = 'London,UK';

// Get the map json data from Google Maps using the $address variable
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';

$json = file_get_contents($googleCall);

$json = json_decode($json);
//header("Content-type: application/json");
//echo $json;

echo $json->results->geometry->location->lat;
echo $json->results->geometry->location->lng;
?>   

Comments

0

use json_decode here documentation http://php.net/manual/en/function.json-decode.php

<?php

// Address for Google to search
$address = 'London,UK';

// Get the map json data from Google Maps using the $address variable
$googleCall = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address .'&sensor=false';

$json = file_get_contents($googleCall);

$json = json_decode($json);


echo $json->results[0]->geometry->location->lat;
echo $json->results[0]->geometry->location->lng;
?>   

2 Comments

Thanks for the help. I had similar code in before, but it didn't display a result. The same is with your code above. Am I reading the JSON data incorrectly?
@PHP-L-Driver i update my answer , i found maps.googleapis.com/maps/api/geocode/…‌​ return data in result[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.