views:

32

answers:

2

I have a requirement to geocode data using Google's geocoding service. Google's geocoding services aren't as friendly to consumption via .NET as, say Bing's (no surprise there) so while I could go all out with ContractDataSerializers, WCF, JSON and a whole other pile of acronyms is there anything wrong with something like the below if all I need is, say, latitude and longitude viz.

string url = String.Format("http://maps.google.com/maps/api/geocode/xml?address=blah&region=ie&sensor=false", HttpUtility.UrlEncode(address));
Coordinates returnValue = new Coordinates();

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(url);
XmlNodeList xmlNodeList = xmlDocument.SelectNodes("/GeocodeResponse/result");

if (xmlNodeList != null)
{
   // Do something here with the information
}

Other than a lot of upfront development effort what precisely will the other approach buy? I am very comfortable with WCF, DataContracts, ServiceContracts etc. but I can't see what they'll bring to the table here...

+1  A: 

Use the GoogleMap Control project on codeplex : http://googlemap.codeplex.com/

It has class for doing geocoding with Google : http://googlemap.codeplex.com/wikipage?title=Google%20Geocoder&referringTitle=Documentation .

Andreas Paulsson
Andreas, The question was what benefit, if any, would going down the WCF and JSON route get me?
noonand
@noonand, yes I am aware of that but since you gave the impression (to me) that you think using Google geocode from .NET is a hassle, I pointed you to an easier way to use it from .NET (solving the original problem). The benifit of going down the WCF, JSON route would be to be able to work with typed classes instead of interpreting XML (you let WCF do that for you). The problem is that it is a lot of work but if you use the component that I link to, that work has already been done.
Andreas Paulsson
That sums it up nicely I think. Many thanks Andreas.
noonand
+1  A: 

I'd use XDocument with WebRequest. Following example might help.

public static GeocoderLocation Locate(string query)
{
    WebRequest request = WebRequest.Create("http://maps.google.com/maps?output=kml&q="
        + HttpUtility.UrlEncode(query));

    using (WebResponse response = request.GetResponse())
    {
        using (Stream stream = response.GetResponseStream())
        {
            XDocument document = XDocument.Load(new StreamReader(stream));

            XNamespace ns = "http://earth.google.com/kml/2.0";

            XElement longitudeElement = document.Descendants(ns + "longitude").FirstOrDefault();
            XElement latitudeElement = document.Descendants(ns + "latitude").FirstOrDefault();

            if (longitudeElement != null && latitudeElement != null)
            {
                return new GeocoderLocation
                {
                    Longitude = Double.Parse(longitudeElement.Value, CultureInfo.InvariantCulture),
                    Latitude = Double.Parse(latitudeElement.Value, CultureInfo.InvariantCulture)
                };
            }
        }
    }

    return null;
}
KMan
From what I see, much the same as mine? You say XDocument, I say XmlDocument, but other than that and the attendent LINQ type code I don't see a whole pile of difference. Please correct me if I'm wrong. The question was what benefit, if any, would going down the WCF and JSON route get me?
noonand
@noonand: What are the other options you are considering besides "WCF and JSON"?
KMan
As I've coded above, Short, sweet, simple - works!
noonand