Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
.NET Zone is brought to you in partnership with:

My name is Pieter De Rycke, I am a Belgian technical .Net architect working in the insurance sector. I am always eager to learn new technologies and I like to keep up with the latest innovations and trends in the IT sector. I am a big believer in web services for interoperability between platforms. Pieter is a DZone MVB and is not an employee of DZone and has posted 16 posts at DZone. You can read more from them at their website. View Full User Profile

Using Google Geocode to get GPS coordinates from an address

02.22.2012
Email
Views: 3633
  • submit to reddit
The .NET Zone is presented by JNBridge and VaraLogix to keep you updated on all the latest news, tips, and tools in the .NET community.  Check out today's top .NET content and read about JNBridge's innovative tools for .NET and Java interoperability and VaraLogix's deployment automation and CM utilities plus their best practices.

For a hobby project, I recently needed to calculate distances between stores and find nearby stores. In order to implement the required functionality, I have used the Google Geocoding service to have GPS coordinates for all my database entries and then it was just a matter of applying the correct math.

The Google Geocoding service is a REST service offered free of charge by Google and no developer sign-up is required. It can translate an address string into GPS coordinates. Result data can be returned in XML or JSON format. The only limitation is that the free version can only geocode 2500 addresses per day.

More technical information can be found on the following web page: http://code.google.com/intl/en-US/apis/maps/documentation/geocoding.

In this blog post, I want to share the tinny wrapper I have created to simplify calling this web service.

I first created a generic interface for the Geocoder.

public interface IGeocoder
{
    Coordinates Geocode(string address);
}

I also created a small data structure for my coordinates.

public class Coordinates
{
    public Coordinates(double latitude, double longitude)
    {
        Latitude = latitude;
        Longitude = longitude;
    }

    public double Latitude { get; private set; }

    public double Longitude { get; private set; }
}

Then I wrote the following implementation that uses the Google web service. I use the XML format for geocoded addresses because XML is easier to parse in .Net applications without requiring 3rd party libraries. If you want to use this service from a WP7 application it would be better to use the more compact JSON format.

public class GoogleGeocoder : IGeocoder
{
    private const string ServiceUri = "http://maps.googleapis.com/maps/api/geocode/xml?address={0}&region=be&sensor=false";

    public Coordinates Geocode(string address)
    {
        if (string.IsNullOrEmpty(address))
            throw new ArgumentNullException("address");

        string requestUriString = string.Format(ServiceUri, Uri.EscapeDataString(address));

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUriString);

        try
        {
            WebResponse response = request.GetResponse();

            XDocument xdoc = XDocument.Load(response.GetResponseStream());

            // Verify the GeocodeResponse status
            string status = xdoc.Element("GeocodeResponse").Element("status").Value;
            ValidateGeocodeResponseStatus(status, address);

            XElement locationElement = xdoc.Element("GeocodeResponse").Element("result").Element("geometry").Element("location");
            double latitude = (double)locationElement.Element("lat");
            double longitude = (double)locationElement.Element("lng");

            return new Coordinates(latitude, longitude);
        }
        catch (WebException ex)
        {
            switch(ex.Status)
            {
                case WebExceptionStatus.NameResolutionFailure:
                    throw new ServiceOfflineException("The Google Maps geocoding service appears to be offline.", ex);
                default:
                    throw;
            }
        }

    }

    private void ValidateGeocodeResponseStatus(string status, string address)
    {
        switch (status)
        {
            case "ZERO_RESULTS":
                string message = string.Format("No coordinates found for address \"{0}\".", address);
                throw new UnknownAddressException(message);
            case "OVER_QUERY_LIMIT":
                throw new OverQueryLimitException();
            case "OK":
                break;
            default:
                throw new Exception("Unkown status code: " + status + ".");
        }
    }
}
References
Published at DZone with permission of Pieter De Rycke, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

This content was brought to you in partnership with JNBridge and VaraLogix JNBridge specializes in .NET and Java interoperability while VaraLogix empowers developers and IT operations with an all-in-one deployment and CM product.  Be sure to view JNBridge's brief video series on accessing Java from .NET and see VaraLogix's tips for taking the risk out of .NET deployment.

Comments

Ajya Chang replied on Wed, 2012/02/29 - 1:48am

Hello,

 

This is a wonderful article. Here I can see that you have explained it with code also. Can I straight away use it? Or I need to go t the reference link provided below for understanding it. THis can be use ful in many we applications the google geocode api. Thanks once again for the code. I will recomment this. Thanks

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.