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();
        p.name = "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
    {
        get
        {
            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

Advertisements

One thought on “Working with Google Earth, part 2.

  1. Pingback: Working with Google Earth, Part 3 – Where is the user looking at? « Coding IT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s