Click here to Skip to main content
Licence CPOL
First Posted 27 May 2008
Views 62,650
Downloads 1,384
Bookmarked 43 times

A .NET API for the Google Maps Geocoder

By | 26 Jun 2008 | Article
A simple .NET library to wrap the Google Maps geocoding functionality

Introduction

Google maps allows you to geocode addresses through their JavaScript API or by directly calling http://maps.google.com/maps/geo?output=xml&key=yourkey&q=someaddress for results in XML. Both of these methods can take time to integrate into your .NET code. To make life a little simpler, I've wrapped the functionality of the HTTP geocode request into a .NET library.

Using the Code

You can make a call to only get all XML in a single string:

string xml = GMapGeocoder.Util.GetXml("123 fake street", "your google map key");

You can also get the code back in the original object structure:

string xml = GMapGeocoder.Util.GetXml("123 fake street", "your google map key");
GMapGeocoder.Generated.kml kml = GMapGeocoder.Util.DeserializeXml(xml);
string fullAddress = kml.Response.Placemark[0].address;
string countryCode = kml.Response.Placemark[0].AddressDetails.Country.CountryNameCode;
string stateCode = kml.Response.Placemark[0].AddressDetails.Country.
                AdministrativeArea.AdministrativeAreaName;

For US addresses, the following calls should make more sense than the kml object above:

GMapGeocoder.Containers.Results results = 
    GMapGeocoder.Util.Geocode("123 fake street", "your google map key");
GMapGeocoder.Containers.USAddress match1 = results.Addresses[0];
string city = match1.City;
string state = match1.StateCode;
double lat = match1.Coordinates.Latitude;

Points of Interest

I didn't find an exact XML definition, so I wrote the XSD file based on documentation on Google, along with results I found from making random queries. I included the XSD file just in case this definition is wrong and code needs to be regenerated. I've also noticed that HttpWebRequest.GetResponse() used by the GetXml call is slow on the initial call from an application, but subsequent calls are much quicker.

History

  • 27th May, 2008: Initial post
  • 24th June, 2008: Article updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sharmil Y Desai

Software Developer (Senior)
Cerner
United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionImprovement to PlacemarkPointToPoint PinmemberGregg Lobdell12:45 6 Jul '11  
QuestionUpdate to Google Geocoding V3? PinmemberGregg Lobdell8:37 6 Jul '11  
AnswerRe: Update to Google Geocoding V3? PinmemberMember 831154920:07 12 Oct '11  
GeneralOnly using aspx.vb PinmemberPaul M Gough22:06 26 Nov '09  
GeneralKML Schema Pinmemberteggen17:13 26 Oct '09  
GeneralI need complete seach results Pinmembersandeepsc2:19 12 Sep '09  
Generalstatus 602 Pinmemberoksanai4:33 25 Jun '09  
GeneralProblem with culture/ Utf8 Pinmemberrouliowiglesias16:07 24 Jun '09  
Generalweb project problem Pinmemberzerointeractive12:29 8 Jun '09  
GeneralVery nice :o) PinmemberCipB4:53 28 May '09  
GeneralRe: Very nice :o) Pinmemberjonranes7:56 28 May '09  
GeneralNice Article Pinmembermbaocha11:31 6 May '09  
GeneralMultiple Addresses PinmemberBlitzPackage9:01 27 Aug '08  
GeneralRe: Multiple Addresses PinmemberSharmil Y Desai5:29 13 Sep '08  
GeneralUK address support Pinmembergeedubb1:08 27 Aug '08  
GeneralConnection refused PinmemberLars Karlsson21:37 3 Jul '08  
GeneralRe: Connection refused PinmemberSharmil Y Desai16:31 7 Jul '08  
GeneralRe: Connection refused PinmemberSharmil Y Desai16:39 7 Jul '08  
Also I'd make sure you're not using a proxy on the deployment box. I think you can add lines to your web.config if you need to specify proxy settings for .Net to use.
GeneralOne more Pinmemberwjcygan6:29 20 Jun '08  
GeneralRe: One more PinmemberSharmil Y Desai7:41 24 Jun '08  
GeneralSlight change Pinmemberwjcygan4:39 20 Jun '08  
GeneralRe: Slight change PinmemberSharmil Y Desai7:38 24 Jun '08  
GeneralError Pinmemberobinna_eke1:24 29 May '08  
GeneralRe: Error PinmemberSharmil Y Desai10:55 29 May '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120604.1 | Last Updated 26 Jun 2008
Article Copyright 2008 by Sharmil Y Desai
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid