How to write a KML file?

latcc

Banned
How would I create a KML text file for Google Earth GPS POIs?

Is there a clever way of doing this or do I simply save text strings to create it?

Has anyone tried it and if so what code did they use?



A typical KML file looks like this in a simple text file...



<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Folder>
<name>London Landmarks</name>
<open>1</open>
<Placemark>
<name>House of Commons</name>
<description></description>
<Point>
<coordinates>0.17810782,1.35190495,0</coordinates>
</Point>
</Placemark>
<Placemark>
</Folder>
</kml>
 
I recommend you to create a template, save it as a text file and add it to the Files tab.
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Folder>
<name>$NAME$</name>
<open>1</open>
<Placemark>
<name>House of Commons</name>
<description></description>
<Point>
<coordinates>$COORDINATES$</coordinates>
</Point>
</Placemark>
<Placemark>
</Folder>
</kml>
[/quotes]

Load the template once when the program starts:
Code: Copy
Dim template As String

template = File.ReadString (File.DirAssets, template.txt)

Create a Sub such as:
Code: Copy
Sub CreateKML (Name As String, Coordinates As String) As String
 Return template.Replace("$NAME$", Name).Replace("$COORDINATES$", Coordinates)
End Sub
 
Upvote 0
That might work for a KML file with just a single Placemark but is no use if more than one Placemark is required.

KML is not a complicated format but it is not simple either.
As the KML standard has been updated, new features have been added.

So a KML file can be a simple one Placemark file or it can be (much) more elaborate.

KML Documentation Introduction - KML - Google Code

The KML posted has no IconStyle(s) defined so any KML viewer (such as Google Earth) will use a plain default icon for the Placemark.

@latcc

How many placemarks do you want in your KML file?

Do you want custom icons?

Do you want to add Polylines and Polygons too?

Post again with more details of what KML you want to create and i'll try and help more.

Martin.
 
Upvote 0
try to export KML points and track

Code: Copy
Sub GetCachedKMLfile (TrackPoints As Map) As String   'TrackPoints in my apps are: Map key - date\time, value is Lat & ";" & Long
Dim sb, track As StringBuilder, coor As String
sb.Initialize
track.Initialize
sb.Append("<?xml version="& QUOTE & "1.0" & QUOTE & " encoding=" & QUOTE & "UTF-8" & QUOTE & "?>").Append(CRLF)
sb.Append("<kml xmlns=" & QUOTE & "http://www.opengis.net/kml/2.2" & QUOTE & ">").Append(CRLF)
sb.Append("<Document>").Append(CRLF)

For i=0 To TrackPoints.Size - 1
   sb.Append("<Placemark>").Append(CRLF)
   sb.Append("<name>" & TrackPoints.GetKeyAt(i) & "</name>").Append(CRLF)
   sb.Append("<description></description>").Append(CRLF)
   sb.Append("<Point>").Append(CRLF)
   coor = DecodeCoords(TrackPoints.GetValueAt(i))   ' Lat and Long separated by ","
   sb.Append("<coordinates>" & coor & "</coordinates>").Append(CRLF)
   sb.Append("</Point>").Append(CRLF)
   sb.Append("</Placemark>").Append(CRLF)
   
   track.Append(coor).Append(",0").Append(CRLF)   'track data including Altitude = 0
Next

sb.Append("<Placemark>").Append(CRLF)
sb.Append("<name>" & "Track" & "</name>").Append(CRLF)
sb.Append("<description></description>").Append(CRLF)
sb.Append("<LineString>").Append(CRLF)
sb.Append("<tessellate>1</tessellate>").Append(CRLF)

sb.Append("<coordinates>" & track.ToString & "</coordinates>").Append(CRLF)

sb.Append("</LineString>").Append(CRLF)
sb.Append("</Placemark>").Append(CRLF)
sb.Append("</Document>").Append(CRLF)
sb.Append("</kml>").Append(CRLF)

'Log (sb.ToString)

Dim fn As String
fn = TrackPoints.GetKeyAt(TrackPoints.Size-1) & ".kml"
fn = fn.Replace(":", "-") 'time stamps with ":" that's not allowed for file naming
File.WriteString(File.DirInternalCache, fn, sb.ToString)
Return fn
End Sub

Sub DecodeCoords (InStr As String) As String 'optional\custom sub for preparing the coords - make it according to your GPS coords data log format
Dim Res(), Finish , p1, p2 As String
Res = Regex.Split(";", InStr)
For i = 0 To Res.Length - 1
   If Res(i).Contains(".") Then
      Finish = Finish & Res(i) & ","
   End If
Next
Finish = Finish.SubString2(0, Finish.Length - 2)
Res = Regex.Split(",", Finish)
Finish = Res(1) & "," & Res(0)
Return Finish
End Sub

Strange, but for the links Lat-Long order is required
Code: Copy
http://maps.google.com/maps/myplaces?ll=" & Me.Latitude & "," & Me.Longitude

But for KML format - otherwise, due to this my DecodeCoords sub so strange :-)
 
Last edited:
Upvote 0
Back
Top