Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Users Online Now
Guests Online 2

Members Online 0

Total Members: 2
Newest Member: NemesisGene

I don't use adfly or similar services, and there are no annoying popup ads here.
But why not save yourself some money by using this Amazon link.

Or if you wish to contribute something to running this site here's the paypal link.

Sign In
Forum Threads
Latest Discussions
  • No Threads created
Hottest Threads
  • No Threads created

Welcome to the Stealthware website.

This site contains various things I have created, from Minecraft mods and tools for Eve Online, to other code snippets, utilities and tutorials.

I will be keeping this up to date with newer Minecraft releases and adding features whenever possible. There will also be many other projects in the future and a few more tutorials on different programming topics.

Use the Articles or Downloads links above to navigate, or check out the important links to the left. The latest News will be shown below or on the Twitter feed to the right.

------   Mod Download Links   ------



Google Earth and using KML

Google Earth and using KML

This article gives a brief introduction to using KML and Google Earth, provides links to more detailed information and contains examples of how to create kml in your application.

Introduction to KML

The first think to know about KML is that it is yet another XML based format. This is very important as it means it is very easy for you and your software to read and manipulate.
The basic structure of a KML file starts with the familiar xml tag and a kml tag with the namespace reference. The main body of the file can contain several different tags. The Document and NetworkLink tags are described here, for more information on the other available tags see the links at the end. The Document tag serves as a container for many other tags and specifies basic information about the content of the file, for example the Name and Description. A Document can contain many tags of various types, the simplest and probably most used is the Placemark as described below.
Adding a Placemark to your KML file will cause Google Earth to display a pin marking the given location on the map. To do this a Placemark tag requires several additional tags to give information about this mark and its location. As with Document the Name and Description tags, as well as many others not mentioned here, provide this functionality. However one of the most important tags to include here is the Point tag, it specifies location information for this Placemark.

="1.0" encoding="UTF-8"?>

  a name for this list
  a useful description
    A name for this place
    Some Description Here


The example above shows a very simple KML file with only one Placemark. As mentioned above the Point tag specifies the location of a Placemark, but it requires the Coordinates tag to actually set the GPS position. Coordinates contains three decimal numbers the represent the Latitude, Longitude and Altitude in that order. Latitude and Longitude need to be in Decimal format rather than Degrees, Minutes and Seconds, an example of how to calculate this is included in the source code or see the references section below.
There are many other tags that can be used within a Placemerk, for example LookAt specifies the position of the camera when the placemark is selected. The source code contains more detailed examples or see the links below for more information.

Linking to Dynamic Content

KML provides a special tag for linking to other files. This can be used to link to static files or with a scripting language like PHP to dynamically generate the KML content. Or even, as in the rest of this article, with a custom web server built into your application. The Example below shows a simple KML file containing a NetworkLink tag.

="1.0" encoding="UTF-8"?>

    This is a Network Link

The NetworkLink tag is similar to Document as it is used at the top level in a KML file and contains the basic tags like Name. However it cannot contain tags such as Placemark, instead it just requires one tag, the Link tag. This is used to specify the link to the file itself within a href tag, and aditional parameters such as RefreshInterval that control if and when the link will be reloaded.

Creating you own Web Server in C#

Writing your own web server may sound like a very complicated thing to attempt and in many languages it would be, but not in C#. Using the HttpListerner class is fairly simple and with a few tweaks it can act as a complete web server.
The example code below shows how to create a HttpListener that will respond to a single request to download the file called icon.png. In order to create a fully working server from this you would need to put the code in a loop to serve multiple requests, and modify it to respond to requests for different files or add a general case to serve any file.

// create a new Http Listener
HttpListener listener = new HttpListener();

// add the ip:ports to listen on

// start the listener

// get the connection context, blocks until browser connects
HttpListenerContext ctx = listener.GetContext();

// set the response code to 200 OK
ctx.Response.StatusCode = 200;

// use the request object to find out what the browser wants
if (ctx.Request.Url.AbsoluteUri.EndsWith("icon.png"))
  // set content type on the response
  ctx.Response.ContentType = "image/png";

  // open the image file and copy all data to the response output stream
  CopyStream(File.OpenRead("icon.png"), ctx.Response.OutputStream);

  // flush the output stream and close response

Customising and Serving KML

With the simple web server you can now have your application provide content to a browser or Google Earth. Serving static content is useful, but not when compared to dynamically generated content. There are many ways to edit or create an XML file in C#. You could use XML Serialisation to create a file based on the data contained in a class in your program. You could use the built in libraries for traversing and manipulating an XML document, or as is done in this example you can simply replace keywords in the file with the required content.

// open the kml file and create in/out writers
StreamWriter writer = new StreamWriter(ctx.Response.OutputStream);
StreamReader read = new StreamReader(File.OpenRead("example.kml"));

while (!read.EndOfStream)
  // read a line from the source file
  string data = read.ReadLine();

  // replace any special tags with the required value
  if (data.Contains(""))
    data = data.Replace("", name);
  if (data.Contains(""))
    data = data.Replace("", name + "" + description + "Browser: "
      + ctx.Request.UserAgent + "Time: " + DateTime.Now.ToLongTimeString());
  if (data.Contains(""))
    data = data.Replace("", "icon.png");

  // and write it out

This code opens the example.kml file and reads the contents, replaces any special markers with the correct value and writes it out to the client. As can be seen in the full source code this is used within the web server to process a request for the example.kml file.

This article is intended to provide you with enough information to start creating your own content using KML, and to publish that from within your own programs. However KML can do so much more than is covered here so I highly recommend you look some of the additional sources of information below, or just search for KML. Equally the web serving abilities of .Net are much greater than mentioned here, in fact it is possible to create a full web server, including the ability to process Asp.Net or even PHP content. There should be some extra details of how to continue extending and using these examples on this site in the future, so keep an eye out.

Downloads & References

Thanks for reading my Google Earth / KML / HttpListener tutorial, I hope it helps you. If you have any comments or questions please use the Post Comment link below or email me.

Originally written January 2008.

Martin 20 August 2011 28241 reads 0 comments 0 ratings Print


Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Rating is available to Members only.
Please Login to vote.
Awesome! (0)0 %
Very Good (0)0 %
Good (0)0 %
Average (0)0 %
Poor (0)0 %