Showing posts with label Google. Show all posts
Showing posts with label Google. Show all posts

Friday, October 03, 2008

Using the Google maps geocode service to find the co-ordinates of an address

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:

googlecode_cmd

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:

http://static.mikehadlow.com/Mike.GoogleGeocode.zip

Monday, August 04, 2008

On Google App Engine

I was just reading Ayende's post 'Thinking About Cloud Computing'. He talks about two very different approaches; Amazon's EC2/GoGrid where you have a VM that sits on Amazon's or GoGrid's servers and Google App Engine where Google provide an application hosting environment. Ayende's take is that the Google approach is the one with legs and I'm inclined to agree with him.

I've been aware of EC2 for a while now because I'm a keen user of S3, but I'd not come across Google App Engine before. I really like the premise; you simply upload your application to the cloud and it just scales as required. At the moment it only supports Python, but this is something that will surely spread to other environments. It can only be a matter of time before someone supplies a Mono based .NET environment. Once that happens Mono will move from being an interesting .NET sideshow to being seriously mainstream.

Up to now, If you're thinking of building the next YouTube or Twitter you've got two choices, you can concentrate on getting a compelling new application out there and hope that you'll be able to deal with scaling it up if it gets popular. Possibly facing a similar to fate to Twitter, which has been having serious scaling issues. Or alternatively, you spend the money up front on infrastructure. Probably wasting money on something that may never fly.

With Cloud computing you don't have this dilemma. You can concentrate on building a fantastic application knowing that you don't have to worry about the infrastructure.

What will Microsoft's response to this be? Their OS monopoly must surely be threatened by a world where anyone can get limitless scalability on a pay-as-you-go basis. Why would anyone every buy a server operating system again? Will the beast soon start selling its own cloud services?