Google Earth KML File Creator Class
Title: Google Earth KML File Creator Class
Author: Ian John Grech
Email:
Member ID: 12345
Language: C# 2.0
Platform: .NET 2.0
Technology:
Level: Beginner
Description: Provides an introductory class to create Google Earth KML files
Section General C#
SubSection C# Samples \ General
- Download demo project - XXX Kb
- Download source - XXX Kb
Introduction
This projects 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 the KMLCreator.cs. This has three classes, KMLCoordinates, KMLPoint and 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 ident 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