This project has moved and is read-only. For the latest updates, please go here.

Retrieve Coordinates from Placemark

Jan 8, 2015 at 7:51 PM

I'm attempting to utilize VB.NET to pull coordinates out of the Placemarks.

Example File:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="">
    <description>Examples of paths. Note that the tessellate tag is by default
      set to 0. If you want to create tessellated lines, they must be authored
      (or edited) directly in KML.</description>
    <Style id="yellowLineGreenPoly">
      <name>Absolute Extruded</name>
      <description>Transparent green wall with yellow outlines</description>
        <coordinates> -112.2550785337791,36.07954952145647,2357
My code so far - I'm having a problem getting to the coordinates innertext:
Dim KMLFileStreamIn As New System.IO.FileStream("Test.kml", IO.FileMode.Open)
        Dim KMLFile As KmlFile = KMLFile.Load(KMLFileStreamIn)
        Dim KMLFileStreamOut As New System.IO.FileStream("TestOut.kml", IO.FileMode.Create)
        Dim Placemark As New Placemark

        Dim kml As Kml = KMLFile.Root
        If Not kml Is Nothing Then

            For Each LineString In kml.Flatten().OfType(Of LineString)()
                TextBox1.Text = LineString.Coordinates.ToString & " " & LineString.Id.ToString & LineString.TargetId.ToString '& Placemark.Id & vbNewLine


            For Each Placemark In kml.Flatten().OfType(Of Placemark)()
                TextBox1.Text = Placemark.Id.ToString & " " & Placemark.TargetId.ToString & vbNewLine  '& Placemark.Id & vbNewLine
Any help is greatly appreciated. Note: I have no Linq experience so if I need to use that can you please direct me towards some easy links - I'm starting to learn and have books but am very much a beginner.

Thank you.
Jan 8, 2015 at 9:21 PM
I think you're nearly there. To access the coordinates of the KML you've posted, you'll need to cast the Geometry property of the placemark to a LineString, something like the following (note I'm not a VB .NET guy so this is only my best guess at how it could look!)
For Each placemark In kml.Flatten().OfType(Of Placemark)()
    Dim lineString As LineString = TryCast(placemark, LineString)
    If lineString Is Not Nothing Then
        Dim coordinates As CoordinateCollection = lineString.Coordinates
        ' Do something with coordinates, such as iterate over it
    End If
Hope it helps,