Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting street,city and country by reverse geocoding using google

I'm trying to getting the $street, $city and $country string from google json. It works for my home address : http://maps.googleapis.com/maps/api/geocode/json?latlng=52.108662,6.307370&sensor=true

$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lng."&sensor=true";
    $data = @file_get_contents($url);
    $jsondata = json_decode($data,true);
    if(is_array($jsondata) && $jsondata['status'] == "OK")
    {
          $city = $jsondata['results']['0']['address_components']['2']['long_name'];
          $country = $jsondata['results']['0']['address_components']['5']['long_name'];
          $street = $jsondata['results']['0']['address_components']['1']['long_name'];
    }

But for a different address with more data in the arrays like this example: http://maps.googleapis.com/maps/api/geocode/json?latlng=52.154184,6.199592&sensor=true it doesn't work, because there is more data in the json array and it makes the province the country.

How can I select the type that I need (long_name)?

  • for street : long_name where "types" : [ "route" ]
  • for city : long_name where "types" : [ "locality", "political" ]
  • for country : long_name where "types" : [ "country", "political" ]

Example output from the geocode JSON:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "89",
               "short_name" : "89",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Wieck De",
               "short_name" : "Wieck De",
               "types" : [ "establishment" ]
            },
            {
               "long_name" : "Industrieweg",
               "short_name" : "Industrieweg",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Zutphen",
               "short_name" : "Zutphen",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Zutphen",
               "short_name" : "Zutphen",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Gelderland",
               "short_name" : "GE",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Nederland",
               "short_name" : "NL",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "7202 CA",
               "short_name" : "7202 CA",
               "types" : [ "postal_code" ]
            }

I think I fixed it myself, hereby my code:

// street
foreach ($jsondata["results"] as $result) {
    foreach ($result["address_components"] as $address) {
        if (in_array("route", $address["types"])) {
            $street = $address["long_name"];
        }
    }
}
// city
foreach ($jsondata["results"] as $result) {
    foreach ($result["address_components"] as $address) {
        if (in_array("locality", $address["types"])) {
            $city = $address["long_name"];
        }
    }
}
// country
foreach ($jsondata["results"] as $result) {
    foreach ($result["address_components"] as $address) {
        if (in_array("country", $address["types"])) {
            $country = $address["long_name"];
        }
    }
}
like image 916
Ruben Avatar asked Aug 11 '13 15:08

Ruben


People also ask

Is Google Maps reverse geocoding free?

It's free as long as you credit them and you need fewer than 15000 lookups per day. You can pay if you need more.

Can you geocode with Google Earth?

Select the fields in your spreadsheet that contain the latitude and longitude data and click Next. Optional: Specify the type of each field in your dataset. Click Finish. Google Earth begins geocoding your data.


2 Answers

You could convert the data to the associative array and work with it like

 $data = array();
 foreach($jsondata['results']['0']['address_components'] as $element){
     $data[ implode(' ',$element['types']) ] = $element['long_name'];
 }
 print_r($data);

 echo 'route: ' . $data['route'] . "\n";
 echo 'country: ' . $data['country political'];
like image 89
lejlot Avatar answered Oct 16 '22 10:10

lejlot


Your code is perfectly good, but wouldn't it be better to use a switch inside 1 foreach instead of repeated foreach loops? Here is how I parse the exact same array :

  $location = array();

  foreach ($result['address_components'] as $component) {

    switch ($component['types']) {
      case in_array('street_number', $component['types']):
        $location['street_number'] = $component['long_name'];
        break;
      case in_array('route', $component['types']):
        $location['street'] = $component['long_name'];
        break;
      case in_array('sublocality', $component['types']):
        $location['sublocality'] = $component['long_name'];
        break;
      case in_array('locality', $component['types']):
        $location['locality'] = $component['long_name'];
        break;
      case in_array('administrative_area_level_2', $component['types']):
        $location['admin_2'] = $component['long_name'];
        break;
      case in_array('administrative_area_level_1', $component['types']):
        $location['admin_1'] = $component['long_name'];
        break;
      case in_array('postal_code', $component['types']):
        $location['postal_code'] = $component['long_name'];
        break;
      case in_array('country', $component['types']):
        $location['country'] = $component['long_name'];
        break;
    }

  }
like image 37
joseph-l Avatar answered Oct 16 '22 10:10

joseph-l