views:

78

answers:

3

Hi all,

I have noticed a lot of information about how to get your location using Google geolocation looks, based on IP. But I am wondering if and how I could use this service to input a location (longitude and latitude) and get back the current address, or at least a city, state.

I would like to do this in C#, but I'll work with any language.

Any advice? Brett

A: 

Google's Maps APIs goes over http, so sending request with get and then parsing the request... It should be possible to do in any language.

For example, in PHP:

$ret = file_get_contents("http://maps.google.com/maps/api/geocode/xml?address=" .
            urlencode($address) .
            "&sensor=false" .
            "&key=" . $this->key
    );

$xml = new SimpleXMLElement($ret);
$error = $xml->status;

Same works for all APIs.

Smar
This is not an implementation of reverse geocoding.
igorw
I meant as example, how whole thing worked... Maybe I should've been more exact. I thought that just showing off how it actually works would be enough.
Smar
+1  A: 

What you describe is called Reverse Geocoding. Google provides a Geocoding Web Service API which you can call from your server-side application (using any language) to do reverse geocoding.

For example, the following request:

http://maps.google.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=false

... will return a response that looks like the following (truncated):

<GeocodeResponse> 
 <status>OK</status> 
 <result> 
  <type>street_address</type> 
  <formatted_address>277 Bedford Ave, Brooklyn, NY 11211, USA</formatted_address> 
  <address_component> 
   <long_name>277</long_name> 
   <short_name>277</short_name> 
   <type>street_number</type> 
  </address_component> 
  <address_component> 
   <long_name>Bedford Ave</long_name> 
   <short_name>Bedford Ave</short_name> 
   <type>route</type> 
  </address_component> 
  <address_component> 
   <long_name>Brooklyn</long_name> 
   <short_name>Brooklyn</short_name> 
   <type>sublocality</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>New York</long_name> 
   <short_name>New York</short_name> 
   <type>locality</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>Kings</long_name> 
   <short_name>Kings</short_name> 
   <type>administrative_area_level_2</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>New York</long_name> 
   <short_name>NY</short_name> 
   <type>administrative_area_level_1</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>United States</long_name> 
   <short_name>US</short_name> 
   <type>country</type> 
   <type>political</type> 
  </address_component> 
  <address_component> 
   <long_name>11211</long_name> 
   <short_name>11211</short_name> 
   <type>postal_code</type> 
  </address_component> 
  <geometry> 
   <location> 
    <lat>40.7142330</lat> 
    <lng>-73.9612910</lng> 
   </location> 
   <location_type>ROOFTOP</location_type> 
   <viewport> 
    <southwest> 
     <lat>40.7110854</lat> 
     <lng>-73.9644386</lng> 
    </southwest> 
    <northeast> 
     <lat>40.7173806</lat> 
     <lng>-73.9581434</lng> 
    </northeast> 
   </viewport> 
  </geometry> 
 </result> 
</GeocodeResponse> 

However be aware that the Google Maps API Terms of Use seem to prohibit the storage of the results, unless the store acts as a cache for data that will used in Google Maps. You may want to get in touch with Google and enquire on the Google Maps API Premier to have more flexible terms of use for your geocoding requirements.

Daniel Vassallo
Works like a charm thanks. I won't be storing the data, so that shouldn't be an issue.Thanks!!!
Brett
A: 

What you are trying to do is reverse geocoding (See Daniel's answer).

An example implementation in PHP:

/**
* reverse geocoding via google maps api
* convert lat/lon into a name
*/
function reverse_geocode($lat, $lon) {
    $url = "http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lon&amp;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;
}
igorw