Reverse GEO lookup in C#

by Mads Kristensen 20. November 2008 02:42

Google’s maps API now supports reversed GEO lookup which allows you to find an address based on geo coordinates.  All you need is a latitude, a longitude and this handy method:

private const string endPoint = "http://maps.google.com/maps/geo?q={0},{1}&output=xml&sensor=true&key=YOURKEY";

 

private static string GetAddress(double latitude, double longitude)

{

  string lat = latitude.ToString(CultureInfo.InvariantCulture);

  string lon = longitude.ToString(CultureInfo.InvariantCulture);

  string url = string.Format(endPoint, lat, lon);

 

  using (WebClient client = new WebClient())

  {

    string xml = client.DownloadString(url);

    XmlDocument doc = new XmlDocument();

    doc.LoadXml(xml);

 

    XmlNode node = doc.ChildNodes[1].FirstChild.ChildNodes[2].ChildNodes[0];

    return node.InnerText;

  }

}

It returns the address as a string.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags: , ,

Server-side

Comments

11/20/2008 5:13:34 AM #

trackback

Reverse GEO lookup in C#

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com |

11/21/2008 1:30:33 AM #

trackback

Reverse GEO Lookup

Reverse GEO Lookup

Suned.dk |

11/28/2008 11:55:07 PM #

Yoann. B

Hi,

Thanks for that article.

I also wrote an article about this subject : blog.sb2.fr/.../...ionGeoTargeting-for-ASPNET.aspx

Yoann. B France |

12/18/2008 3:32:23 AM #

Andrés Zacchino

Great, Gracias

una cosa nomas. error code.

private static string GetAddress(double latitude, double longitude)
{
string lat = latitude.ToString(CultureInfo.InvariantCulture);
string lon = longitude.ToString(CultureInfo.InvariantCulture);
string url = string.Format(endPoint, lat, lon);

using (WebClient client = new WebClient())
{
    string xml = client.DownloadString(url);
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);

    XmlNode nodeStatus = doc.ChildNodes[1].FirstChild.ChildNodes[1].ChildNodes[0];
    if (nodeStatus.InnerText == "200") // if status code is OK
    {
       XmlNode node = doc.ChildNodes[1].FirstChild.ChildNodes[2].ChildNodes[0];
       return node.InnerText; // Primera Direccion que resulve
    }
    else
        return "Not Found";
}
}

Andrés Zacchino Argentina |

1/15/2009 12:01:25 AM #

trackback

extension

extension

Name of the blog |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008