Click here to Skip to main content
11,410,750 members (63,293 online)
Click here to Skip to main content

A .NET API for the Google Maps Geocoder

, 26 Jun 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple .NET library to wrap the Google Maps geocoding functionality


Google maps allows you to geocode addresses through their JavaScript API or by directly calling 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.

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.


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


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
No Biography provided

Comments and Discussions

QuestionThis is useful but Google API has been changed PinmemberJoeSchmoe0074-Jun-14 5:25 
QuestionImprovement to PlacemarkPointToPoint PinmemberGregg Lobdell6-Jul-11 13:45 
QuestionUpdate to Google Geocoding V3? PinmemberGregg Lobdell6-Jul-11 9:37 
AnswerRe: Update to Google Geocoding V3? PinmemberMember 831154912-Oct-11 21:07 
GeneralOnly using aspx.vb PinmemberPaul M Gough26-Nov-09 23:06 
GeneralKML Schema Pinmemberteggen26-Oct-09 18:13 
GeneralI need complete seach results Pinmembersandeepsc12-Sep-09 3:19 
Generalstatus 602 Pinmemberoksanai25-Jun-09 5:33 
GeneralProblem with culture/ Utf8 Pinmemberrouliowiglesias24-Jun-09 17:07 

I had some trouble with the culture and UTF-8 encoding.
I advise you to change:

public static Containers.Point PlacemarkPointToPoint(Generated.Point point)
// Parse result independently of the culture
p.Longitude = Double.Parse(point.coordinates.Split(new char[] { ',' })[0], CultureInfo.InvariantCulture.NumberFormat);
p.Latitude = Double.Parse(point.coordinates.Split(new char[] { ',' })[1], CultureInfo.InvariantCulture.NumberFormat);


public static string GetXml(string address, string key)
//Add utf8 argument
string url = string.Format("{0}&key={1}&oe=utf8", HttpUtility.UrlEncode(address), key);

public static string GetXml(string address, string key)
//Add Utf8
using (StreamReader readStream = new StreamReader(stream, Encoding.UTF8))
return readStream.ReadToEnd();

It's run correctly on some manuel test. I will try your code on a big test case Smile | :)
Thank you for your code and the time saving. Smile | :)
Generalweb project problem Pinmemberzerointeractive8-Jun-09 13:29 
GeneralVery nice :o) PinmemberCipB28-May-09 5:53 
GeneralRe: Very nice :o) Pinmemberjonranes28-May-09 8:56 
GeneralNice Article Pinmembermbaocha6-May-09 12:31 
GeneralMultiple Addresses PinmemberBlitzPackage27-Aug-08 10:01 
GeneralRe: Multiple Addresses PinmemberSharmil Y Desai13-Sep-08 6:29 
GeneralUK address support Pinmembergeedubb27-Aug-08 2:08 
GeneralConnection refused PinmemberLars Karlsson3-Jul-08 22:37 
GeneralRe: Connection refused PinmemberSharmil Y Desai7-Jul-08 17:31 
GeneralRe: Connection refused PinmemberSharmil Y Desai7-Jul-08 17:39 
GeneralOne more Pinmemberwjcygan20-Jun-08 7:29 
GeneralRe: One more PinmemberSharmil Y Desai24-Jun-08 8:41 
GeneralSlight change Pinmemberwjcygan20-Jun-08 5:39 
GeneralRe: Slight change PinmemberSharmil Y Desai24-Jun-08 8:38 
GeneralError Pinmemberobinna_eke29-May-08 2:24 
GeneralRe: Error PinmemberSharmil Y Desai29-May-08 11:55 

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.

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