Working with Google Earth, part 2.

So, in the previous post we got our model sorted. Now we are getting to the interesting part – creating a network-link to Google Earth. “I thought we were creating KML files?” I hear you ask (with my amazing powers of hearing-imaginary-people-questioning-my-every-action). Ah, but who wants boring old static files in this day and age of web 3.0 and Friendface?

So what’s a network link? Basically, it’s a way of serving the KML markup straight to Google Earth through HTTP, rather than creating a KML file and sending that. GE tells you what it is looking at, and you generate a location-appropriate response. Then, as the user moves around the globe, GE keeps asking for new information.

The huge advantage here is that you can serve very small chunks of data at a time, at a resolution appropriate to their zoom level. Also, it works really well for data which updates frequently – think about plotting the current location of a GPS device or something.

So we’re going to implement a KML server, using a HTTP handler in ASP.NET (which we will henceforth call our “endpoint”). HTTP handlers are really useful for implementing things like RSS feeds, KML feeds, things like that.

Create a blank ASP.NET website and add a new Generic Handler (an ashx file). Add the KML model file you created previously into the App_Code folder (you might need to add this as well).

This is the code for the handler. Note that it’s not quite as easy to write as I was expecting, as the generated model class is not as friendly as I was hoping. I might look at wrapping this up in an API in a further post.

using System;
using System.Web;
using System.IO;
using System.Xml.Serialization;

public class MyKmlHandler : IHttpHandler

    public void ProcessRequest(HttpContext context)
        context.Response.ContentType = "text/plain";
        XmlSerializer xSerializer = new XmlSerializer(typeof(KmlType));

        KmlType kml = new KmlType();
        DocumentType doc = new DocumentType();
        PlacemarkType p = new PlacemarkType(); = "Buckingham Palace, London";
        p.address = "Buckingham Palace, London";
        p.Item1 = new PointType();
        ((PointType)p.Item1).coordinates = "-0.1408270289907088,51.50164111165839";
        kml.Item = doc;
        doc.Items1 = new FeatureType[1];
        doc.Items1[0] = p;
        xSerializer.Serialize(context.Response.OutputStream, kml);

    public bool IsReusable
            return false;

To add this to Google Earth is simplicity itself. Simply right-click on “Temporary places”, select “Add” → “Network Link…” and follow the instructions.
Adding a network link step #1

Make sure to select the Refresh tab, and update the “View-based Refresh” to “After Camera Stops” as shown. Now, when you make changes to the feed, simply move the GE camera slightly and the changes will be visible. This will also be important in part 3, where we discuss only returning the data in view.
Note that your “Link” URL will likely be different to mine – it depends on the name of your handler, your web-site, and the port it is running on.
Adding a network link step #2

Our network link is up and running, and Buckingham Palace is shown on the map. Hooray!
Adding a network link step #3


Working with Google Earth

This is the first in a series looking at how to work with KML files, the file format used by Google Earth. KML is an XML format and, rather than writing lots of XPath queries, it would be nice to generate classes that can be serialised to KML. This way, we get intellisense, and compile-time checking of our code. This part looks at generating classes from the KML schema, to make our lives much easier going forwards.

We will be using the XML Schema Definition Tool (xsd.exe) to generate the .NET classes. We will be outputting in C#, but most languages are supported through the /language option.

Firstly, download the schema itself, and save it to your local disk. I’m saving it to C:\source\kml.xsd.

Then, open up a command prompt and run the following command:

   xsd.exe c:\source\kml.xsd /classes /language:cs /out:c:\source

This will generate a “kml.cs” file containing classes representing the schema, ready to add to the project we will be starting soon.