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:
List<KMLCoordinates> l = new List<KMLCoordinates>();
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));
KMLLine kl = new KMLLine(l, "LineSample", "This is a Line Sample");
kl.Save();
KMLCoordinates p = new KMLCoordinates(0.25, 51.25);
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