I've been having a lot of fun playing with Google maps this week. One of things we need to be able to do is map an address to a longitude and latitude so that we can display a point on a google map. This is very easy to do. Simply send a GET request to http://maps.google.com/maps/geo?q=<the address>&output=xml&key=<your key>. This returns a 'kml' XML document containing the geocode information. I simply used Visual Studio to create an XSD from the XML and then XSD.exe to generate the C# types to deserialize the XML document into. Here's the code:
using System;using System.IO;using System.Net;using System.Web;using System.Xml.Serialization;namespace Mike.GoogleGeocode{class Program{private const string googleGeocodeUrl = "http://maps.google.com/maps/geo?q={0}&output=xml&key={1}";private const string proxyUrl = "your proxy address";private const string key = "your key";static void Main(){const string address = "St. Julians, Sevenoaks, Kent, UK";var kml = GetKml(address);Console.WriteLine("Co-ordinates: {0}, for address: {1}", kml.Response.Placemark.Point.coordinates, address);}private static kml GetKml(string address){var url = string.Format(googleGeocodeUrl, HttpUtility.UrlEncode(address), key);var request = (HttpWebRequest)WebRequest.Create(url);request.Accept = "text/xml";request.Method = "GET";// set the proxy if needed.request.Proxy = new WebProxy(proxyUrl, true, new string[0],CredentialCache.DefaultNetworkCredentials);kml kml;using (var response = request.GetResponse())using (var responseStream = new StreamReader(response.GetResponseStream())){try{var serializer = new XmlSerializer(typeof(kml));kml = (kml)serializer.Deserialize(responseStream);}catch (Exception){kml = null;}}return kml;}}}
Running it gives this result:
Which is the right location. One thing to watch out for is that the coordinates property is given as longitude, latitude, but Google maps expects you to enter co-ordinates as latitude, longitude. I was wondering why the API was resolving all my UK addresses to be off the East coast of Africa until I worked that out :P
You can download the full test solution here: