Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geo Coding Address - get district of a certain address (Google API)

I have a database with exact addresses (street, no, city, region/area, country). However, I was wondering if there was a way to use Google API to get the district of a city (e.g. "Manhattan") if we are in New York?

All the other information I have already in the database, so I would just need the district if there is one (of course this will be only in larger cities)...

UPDATE:

I found this function on http://www.techques.com/question/1-3151450/Google-geolocation-API---Use-longitude-and-latitude-to-get-address and tried to change formatted_address to sublocality (even others like short_name etc.) but it does not return anything... any help would be highly appreciated! Thank you!!

function reverse_geocode($lat, $lon) {
    $url = "http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lon&sensor=false";
    $data = json_decode(file_get_contents($url));
    if (!isset($data->results[0]->formatted_address)){
        return "unknown Place";
    }
    return $data->results[0]->formatted_address;
}
like image 574
Helmut Avatar asked Apr 17 '12 21:04

Helmut


People also ask

How do I find the geocode of an address?

Geocoder geocoder = new Geocoder(this, Locale. getDefault()); List<Address> addresses = geocoder.

How do I get Google geolocation API?

Go to the Google Maps Platform > Credentials page. On the Credentials page, click Create credentials > API key. The API key created dialog displays your newly created API key. Click Close.

How do I use Google geocoding API?

Enable Geocoding API In your Google Cloud Platform Console, go to APIs & Services → Dashboard → Enable APIs & Services at the top and choose Maps JavaScript API from the API Library. This will open up the Maps JavaScript API page and Enable it.


2 Answers

You can access the sublocality like this:

function reverse_geocode($lat, $lon) {
    $url = "http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lon&sensor=false";
    $data = json_decode(file_get_contents($url));
    if (!isset($data->results[0]->address_components)){
        return "unknown Place";
    }

    if ($data->results[0]->address_components[2]->types[0]=="sublocality") {

        $return_array['type']="sublocality";
        $return_array['sublocality_long_name']=$data->results[0]->address_components[2]->long_name;
        $return_array['sublocality_short_name']=$data->results[0]->address_components[2]->short_name;

        return $return_array;
        }

}
like image 54
Chris Avatar answered Oct 06 '22 04:10

Chris


You'll find this information inside a geocode-request, when there exists a result with the types set to

 [ "sublocality", "political" ]

Example: 317 Madison Ave,New York City


Modification of the function above for easy access of the response-components:

  /**
    * @param $a mixed latitude or address
    * @param $b mixed optional longitude when $a is latitude
    * @return object geocoding-data
    **/

    function geocode($a, $b=null) {
    $params=array('sensor'=>'false');
    if(is_null($b))
    {
      $params['address']=$a;
    }
    else
    {
      $params['latlng']=implode(',',array($a,$b));
    }
    $url = 'http://maps.google.com/maps/api/geocode/json?'.http_build_query($params,'','&');
    $result=@file_get_contents($url);

     $response=new StdClass;
     $response->street_address               = null;
     $response->route                        = null;
     $response->country                     = null;
     $response->administrative_area_level_1 = null;
     $response->administrative_area_level_2 = null;
     $response->administrative_area_level_3 = null;
     $response->locality                    = null;
     $response->sublocality                 = null;
     $response->neighborhood                = null;
     $response->postal_code                 = null;
     $response->formatted_address           = null;
     $response->latitude                    = null;
     $response->longitude                   = null;
     $response->status                      = 'ERROR';

    if($result)
    {
      $json=json_decode($result);
      $response->status=$json->status;
      if($response->status=='OK')
      {
        $response->formatted_address=$json->results[0]->formatted_address;
        $response->latitude=$json->results[0]->geometry->location->lat;
        $response->longitude=$json->results[0]->geometry->location->lng;

        foreach($json->results[0]->address_components as $value)
        {
          if(array_key_exists($value->types[0],$response))
          {
            $response->{$value->types[0]}=$value->long_name;
          }
        }
      }
    }
  return $response;
}

//sample usage
echo '<hr/>'.geocode('317 Madison Ave,New York City')->sublocality;
  //Manhattan

echo '<hr/>'.geocode('foobar')->status;
  //ZERO_RESULTS

echo '<hr/>'.geocode('40.689758, -74.04513800000001')->formatted_address;
  //1 Liberty Is, Brooklyn, NY 11231, USA
like image 30
Dr.Molle Avatar answered Oct 06 '22 04:10

Dr.Molle