I\'m fairly new to JSON, and I\'m trying to get the latitude and longitude of a geocoded city from the Google Maps API using curl. The function I\'m using is:
Note that it seems that results
contains an array (with only one item in it, here) of results ; and geometry
is one item inside one result.
Here, you can see that results' content is delimited by []
-- which indicates it's an array.
So, you have to first access the first result : $geoloc['results'][0]
Inside of which you'll have the geometry : $geoloc['results'][0]['geometry']
Which will allow you to get the latitude and longitude :
var_dump($geoloc['results'][0]['geometry']['location']['lat']);
var_dump($geoloc['results'][0]['geometry']['location']['lng']);
I suppose that, depending on the address you've searched on, you will sometimes have more than one item in the results
array.
You just need this :
$fullurl = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true";
$string .= file_get_contents($fullurl); // get json content
$json_a = json_decode($string, true); //json decoder
echo $json_a['results'][0]['geometry']['location']['lat']; // get lat for json
echo $json_a['results'][0]['geometry']['location']['lng']; // get ing for json