Using Reverse Geocoding to Find an Address

June 22, 2011

tags: ,
no comments

Using Reverse Geocoding to Find an Address

I had a request from a colleague of mine to help him with a geocoding problem. The colleague needed to find an Making a Reverse Geocoding to Find an Addressaddress by a given latitude and longitude which were supplied by a smartphone consumer. The example in this post will show you how you can use Google geocoding API in order to achieve that using C#.

Reverse Geocoding (Address Lookup)

Geocoding refers to translating an address into its location or coordinates. The reverse of it is address lookup which occurs when you have the map location and you want to get an address out of it. In today’s mobile development it is very common to use the smartphone built-in GPS’ in order to make geocoding and reverse geocoding. When you want to make a geocoding request you can use Google’s geocode service in order to make location queries. When you want to make a geocode query you will have to send a Http request to http://maps.googleapis.com/maps/api/geocode/ with xml or Json format and a bunch of parameters in order to get your desired information. For further reading about making geocode requests go to Google Geocoding API documentation.

Reverse Geocoding Example

Here is a console application example of using reverse geocoding in order to find an address:

class Program
{
  static string baseUri = "http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false";
 
  static void Main(string[] args)
  {
    RetrieveFormatedAddress("51.962146", "7.602304");
    Console.ReadLine();
  }
 
  public static void RetrieveFormatedAddress(string lat, string lng)
  {
    string requestUri = string.Format(baseUri, lat, lng);
 
    using (WebClient wc = new WebClient())
    {
      wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
      wc.DownloadStringAsync(new Uri(requestUri));
    }
  }
 
  static void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
  {
    var xmlElm = XElement.Parse(e.Result);
 
    var status = (from elm in xmlElm.Descendants()
                  where elm.Name == "status"
                  select elm).FirstOrDefault();
    if (status.Value.ToLower() == "ok")
    {
      var res = (from elm in xmlElm.Descendants()
                 where elm.Name == "formatted_address"
                 select elm).FirstOrDefault();
      Console.WriteLine(res.Value);
    }
    else
    {
      Console.WriteLine("No Address Found");
    }
  }
}

As you can see I use a WebClient object to make a request to Google geocoding API with a given latitude and longitude parameters. The returned string is in Xml format (but might be in Json also) which might look like:

<GeocodeResponse>
  <status>OK</status>
  <result>
    <type>street_address</type>
    <formatted_address>1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA</formatted_address>
    <address_component>
      <long_name>1600</long_name>
      <short_name>1600</short_name>
      <type>street_number</type>
    </address_component>
    <address_component>
      <long_name>Amphitheatre Pkwy</long_name>
      <short_name>Amphitheatre Pkwy</short_name>
      <type>route</type>
    </address_component>
    <address_component>
      <long_name>Mountain View</long_name>
      <short_name>Mountain View</short_name>
      <type>locality</type>
      <type>political</type>
    </address_component>
    <address_component>
      <long_name>San Jose</long_name>
      <short_name>San Jose</short_name>
      <type>administrative_area_level_3</type>
      <type>political</type>
    </address_component>
    <address_component>
      <long_name>Santa Clara</long_name>
      <short_name>Santa Clara</short_name>
      <type>administrative_area_level_2</type>
      <type>political</type>
    </address_component>
    <address_component>
      <long_name>California</long_name>
      <short_name>CA</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>94043</long_name>
      <short_name>94043</short_name>
      <type>postal_code</type>
    </address_component>
    <geometry>
      <location>
        <lat>37.4217550</lat>
        <lng>-122.0846330</lng>
      </location>
      <location_type>ROOFTOP</location_type>
      <viewport>
        <southwest>
          <lat>37.4188514</lat>
          <lng>-122.0874526</lng>
        </southwest>
        <northeast>
          <lat>37.4251466</lat>
          <lng>-122.0811574</lng>
        </northeast>
      </viewport>
    </geometry>
  </result>
</GeocodeResponse>

I use LINQ to XML in order to check the response status and if it’s OK I retrieve the formatted address value. Pay attention that I make an asynchronous request since I need to relay on a third party server (which is Google geocoding API in my example).

Summary

The post showed how to use a geocode API in order to make an address lookup according to a given location. I hope it will help you when you need to implement such a thing.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>