Click here to Skip to main content
12,630,137 members (26,552 online)
Click here to Skip to main content
Add your own
alternative version

Stats

97.1K views
2.9K downloads
47 bookmarked
Posted

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

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)

Share

About the Author

Sharmil Y Desai
Software Developer (Senior) Cerner
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionThis is useful but Google API has been changed Pin
JoeSchmoe0074-Jun-14 5:25
memberJoeSchmoe0074-Jun-14 5:25 
QuestionImprovement to PlacemarkPointToPoint Pin
Gregg Lobdell6-Jul-11 13:45
memberGregg Lobdell6-Jul-11 13:45 
QuestionUpdate to Google Geocoding V3? Pin
Gregg Lobdell6-Jul-11 9:37
memberGregg Lobdell6-Jul-11 9:37 
AnswerRe: Update to Google Geocoding V3? Pin
Member 831154912-Oct-11 21:07
memberMember 831154912-Oct-11 21:07 
GeneralOnly using aspx.vb Pin
Paul M Gough26-Nov-09 23:06
memberPaul M Gough26-Nov-09 23:06 
GeneralKML Schema Pin
teggen26-Oct-09 18:13
memberteggen26-Oct-09 18:13 
GeneralI need complete seach results Pin
sandeepsc12-Sep-09 3:19
membersandeepsc12-Sep-09 3:19 
Generalstatus 602 Pin
oksanai25-Jun-09 5:33
memberoksanai25-Jun-09 5:33 
GeneralProblem with culture/ Utf8 Pin
rouliowiglesias24-Jun-09 17:07
memberrouliowiglesias24-Jun-09 17:07 
Generalweb project problem Pin
zerointeractive8-Jun-09 13:29
memberzerointeractive8-Jun-09 13:29 
GeneralVery nice :o) Pin
CipB28-May-09 5:53
memberCipB28-May-09 5:53 
GeneralRe: Very nice :o) Pin
jonranes28-May-09 8:56
memberjonranes28-May-09 8:56 
GeneralNice Article Pin
mbaocha6-May-09 12:31
membermbaocha6-May-09 12:31 
GeneralMultiple Addresses Pin
BlitzPackage27-Aug-08 10:01
memberBlitzPackage27-Aug-08 10:01 
GeneralRe: Multiple Addresses Pin
Sharmil Y Desai13-Sep-08 6:29
memberSharmil Y Desai13-Sep-08 6:29 
GeneralUK address support Pin
geedubb27-Aug-08 2:08
membergeedubb27-Aug-08 2:08 
GeneralConnection refused Pin
Lars Karlsson3-Jul-08 22:37
memberLars Karlsson3-Jul-08 22:37 
GeneralRe: Connection refused Pin
Sharmil Y Desai7-Jul-08 17:31
memberSharmil Y Desai7-Jul-08 17:31 
GeneralRe: Connection refused Pin
Sharmil Y Desai7-Jul-08 17:39
memberSharmil Y Desai7-Jul-08 17:39 
GeneralOne more Pin
wjcygan20-Jun-08 7:29
memberwjcygan20-Jun-08 7:29 
GeneralRe: One more Pin
Sharmil Y Desai24-Jun-08 8:41
memberSharmil Y Desai24-Jun-08 8:41 
GeneralSlight change Pin
wjcygan20-Jun-08 5:39
memberwjcygan20-Jun-08 5:39 
GeneralRe: Slight change Pin
Sharmil Y Desai24-Jun-08 8:38
memberSharmil Y Desai24-Jun-08 8:38 
GeneralError Pin
obinna_eke29-May-08 2:24
memberobinna_eke29-May-08 2:24 
Do you know why I am getting this error? I have my Google key as well


Server Error in '/SeeAjax' Application.
The remote name could not be resolved: 'maps.google.com'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote name could not be resolved: 'maps.google.com'

Source Error:

Line 15: protected void Page_Load(object sender, EventArgs e)
Line 16: {
Line 17: string xml = GMapGeocoder.Util.GetXml("35 Greville Court Napoleon Road London E5 8TF", "ABQIAAAA_LYa_-GfNtQyBdJspQIboBRa7FnI_mqQJH5Raddl725svEZDkRSDjPurwcgJXErd6JKjFzLWFUbDGg");
Line 18: Response.Write(xml);
Line 19: }


Source File: c:\Inetpub\wwwroot\customers\SeeAjax\Geocode.aspx.cs Line: 17

Stack Trace:

[WebException: The remote name could not be resolved: 'maps.google.com']
System.Net.HttpWebRequest.GetResponse() +1502043
GMapGeocoder.Util.GetXml(String address, String key) +134
Geocode.Page_Load(Object sender, EventArgs e) in c:\Inetpub\wwwroot\customers\SeeAjax\Geocode.aspx.cs:17
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061


Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42
GeneralRe: Error Pin
Sharmil Y Desai29-May-08 11:55
memberSharmil Y Desai29-May-08 11:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web02 | 2.8.161205.3 | Last Updated 26 Jun 2008
Article Copyright 2008 by Sharmil Y Desai
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid