Click here to Skip to main content
Click here to Skip to main content

A .NET API for the Google Maps Geocoder

By , 26 Jun 2008
 

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

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionImprovement to PlacemarkPointToPointmemberGregg Lobdell6-Jul-11 12:45 
QuestionUpdate to Google Geocoding V3?memberGregg Lobdell6-Jul-11 8:37 
AnswerRe: Update to Google Geocoding V3?memberMember 831154912-Oct-11 20:07 
GeneralOnly using aspx.vbmemberPaul M Gough26-Nov-09 22:06 
GeneralKML Schemamemberteggen26-Oct-09 17:13 
GeneralI need complete seach resultsmembersandeepsc12-Sep-09 2:19 
Generalstatus 602memberoksanai25-Jun-09 4:33 
GeneralProblem with culture/ Utf8memberrouliowiglesias24-Jun-09 16:07 
Generalweb project problemmemberzerointeractive8-Jun-09 12:29 
GeneralVery nice :o)memberCipB28-May-09 4:53 
GeneralRe: Very nice :o)memberjonranes28-May-09 7:56 
GeneralNice Articlemembermbaocha6-May-09 11:31 
GeneralMultiple AddressesmemberBlitzPackage27-Aug-08 9:01 
GeneralRe: Multiple AddressesmemberSharmil Y Desai13-Sep-08 5:29 
GeneralUK address supportmembergeedubb27-Aug-08 1:08 
GeneralConnection refusedmemberLars Karlsson3-Jul-08 21:37 
GeneralRe: Connection refusedmemberSharmil Y Desai7-Jul-08 16:31 
GeneralRe: Connection refusedmemberSharmil Y Desai7-Jul-08 16:39 
GeneralOne morememberwjcygan20-Jun-08 6:29 
GeneralRe: One morememberSharmil Y Desai24-Jun-08 7:41 
GeneralSlight changememberwjcygan20-Jun-08 4:39 
GeneralRe: Slight changememberSharmil Y Desai24-Jun-08 7:38 
GeneralErrormemberobinna_eke29-May-08 1:24 
GeneralRe: ErrormemberSharmil Y Desai29-May-08 10:55 
That's an odd error, seems more like a DNS issues where the box this is running on can't find maps.google.com . Can you get to http://maps.google.com/maps/geo?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA&output=xml&key=ABQIAAAA_LYa_-GfNtQyBdJspQIboBRa7FnI_mqQJH5Raddl725svEZDkRSDjPurwcgJXErd6JKjFzLWFUbDGg directly from your browser on the box running the application?
 
Tried your exact call on my machine with the library and it seems to be working fine even though google can't resolve that address.
 
I get a 602 response back:
 
<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>35 Greville Court Napoleon Road London E5 8TF</name><Status><code>602</code><request>geocode</request></Status></Response></kml>

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

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