Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Google Earth KML File Creator Class

0.00/5 (No votes)
22 Apr 2007 1  
A class to create XML files for use with Google Earth

Introduction

This project provides a very simple class that can be used to create Google Earth KML Files. Only two formats are shown in the code, namely Point and LineString, but these should provide enough background to allow anyone to continue coding their own KML files based on the KML Object Model.

Reference

The KML Reference is available here.

Using the Code

The core of the project is KMLCreator.cs. This has three classes:

  • KMLCoordinates
  • KMLPoint
  • KMLLine

KMLCoordinates is used by both KMLPoint and KMLLine. It contains the Longitude, Latitude and Altitude of a specific coordinate in space.

KMLPoint will create a Pin with location specified by the KMLCoordinates.

KMLLine will create a Line with locations specified by a Generic List of KMLCoordinates.

The classes have some hard coded attributes which a developer can amend if and as required. Coding is very basic and should be easy to understand. the 'hardest' part is the use of the XMLTextWriter class.

Note: In writing, I used curly braces to help segregate and indent the code in the same manner as it should appear in XML. This is for no particular reason other than to help visualize the output during coding.

Typical usage is as follows:

//Generic List of KML Coords
List<KMLCoordinates> l = new List<KMLCoordinates>();

//Sample KML Coords for a Line
l.Add(new KMLCoordinates(0.0, 51.0));
l.Add(new KMLCoordinates(0.2, 51.01));
l.Add(new KMLCoordinates(-0.1, 51.03));
l.Add(new KMLCoordinates(0.03, 51.01));
l.Add(new KMLCoordinates(-0.5, 51.05));
l.Add(new KMLCoordinates(0.5, 51.05));
l.Add(new KMLCoordinates(0.6, 51.06));

//Creates a Line file as C:\Line.kml
KMLLine kl = new KMLLine(l, "LineSample", "This is a Line Sample");
kl.Save();

//Sample KML Coord for a Point
KMLCoordinates p = new KMLCoordinates(0.25, 51.25);

//Creates a Point file as C:\Point.kml
KMLPoint kp = new KMLPoint(p, "PointSample", 
                              "This is a Point Sample");
kp.Save();

History

  • Revision 1.0 dated 2007/04/22 – First draft of project

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here