23

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:

function geocode($city){
   $cityclean = str_replace (" ", "+", $city);
   $details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $cityclean . "&sensor=false";

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $details_url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $geoloc = json_decode(curl_exec($ch), true);

   $step1 = $geoloc['results'];
   $step2 = $step1['geometry'];
   $coords = $step2['location'];

   print $coords['lat'];
   print $coords['lng'];

}

The goal of all of that is to pull the lat and lng values from the Results -> Geometry -> Location array of the following JSON:

{
  "status": "OK",
  "results": [ {
    "types": [ "locality", "political" ],
    "formatted_address": "Westminster, London, UK",
    "address_components": [ {
      "long_name": "London",
      "short_name": "London",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Westminster",
      "short_name": "Westminster",
      "types": [ "administrative_area_level_3", "political" ]
    }, {
      "long_name": "Greater London",
      "short_name": "Greater London",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "England",
      "short_name": "England",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United Kingdom",
      "short_name": "GB",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 51.5001524,
        "lng": -0.1262362
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 51.3493528,
          "lng": -0.3783580
        },
        "northeast": {
          "lat": 51.7040647,
          "lng": 0.1502295
        }
      },
      "bounds": {
        "southwest": {
          "lat": 51.3493528,
          "lng": -0.3783580
        },
        "northeast": {
          "lat": 51.7040647,
          "lng": 0.1502295
        }
      }
    }
  } ]
}

However, it doesn't print anything. I know that the function successfully retrieves the JSON from Google and brings it to my server, as I did a print statement for the 'status' value and it returned 'OK'. The problem seems to be when I try and delve deeper into the JSON.

Sorry if it's a straightforward problem, but like I said, I'm new to this and it's driving me crazy now.

Thanks a lot! :)

2 Answers 2

20

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.

1
  • Hi, is there way to get lat long using ip of the user ? May 26, 2014 at 10:50
20

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
1
  • 2
    that may work but he asked about curl, not file_get_contents.
    – Ryan Ore
    Nov 30, 2012 at 20:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.