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