Geeks With Blogs


Add to Google

Tim Hibbard CEO for EnGraph software

I got an email today from somebody that wants the source code for the "Fly to with Google Earth" function on Where's Tim. Essential, I created a base XML file, used an XMLNodeList to parse and change the base XML file, saved the base XML as a KML file, and used a Response.Redirect to launch the url with the KML file.

Here is the base XML file that I used. Note that the file HAS to be saved with the .xml extension.
<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="">
    <Snippet>Tim Hibbard's real time location</Snippet>
    <description>Location of Tim Hibbard, Software Architect for EnGraph</description>
    <name>Tim Hibbard</name>
      <longitude>Replace Me later</longitude>
      <latitude>Replace Me later</latitude>
          <href> your own url here)</href>
     <coordinates>replace with lat,replace with lon,0</coordinates>

I then use this function to populate the XML document:

Private Function GenerateKML(ByVal lat as Double, ByVal lon as Double) As String
    Dim xDoc As New System.Xml.XmlDocument
   'Change this url to where you saved your base XML doc

    Dim nowticks As String = Now.Ticks.ToString
   'creates a new unique name to save
    Dim tmpFileName As String = Server.MapPath("kml\") & nowticks & ".xml"

   'replaces the longitude tag with the lon parameter
   Dim MyNode As System.Xml.XmlNodeList = xDoc.GetElementsByTagName("longitude")
   MyNode.Item(0).InnerText = lon.ToString

   'replaces the latitude tag with the lon parameter
   MyNode = xDoc.GetElementsByTagName("latitude")
   MyNode.Item(0).InnerText = lat.ToString

   'replaces the coordinates tag with actual data
   MyNode = xDoc.GetElementsByTagName("coordinates")
   MyNode.Item(0).InnerText = lon.ToString & ", " & lat.ToString & ", 0"

   'saves the file as an XML document
   Dim actualFileName As String = tmp.TrimEnd(".xml".ToCharArray) & ".kml"
   'renames the file as a KML file that Google Earth can understand
   Rename(tmpFileName, actualFileName)

   Return "" & nowticks & ".kml"

End Function

Please let me know if this helps or if you use it in an application.

Posted on Tuesday, October 4, 2005 4:32 PM EnGraph , .NET , GPS , Where's Tim | Back to top

Comments on this post: KML Creator source code

# re: KML Creator source code
Requesting Gravatar...
I need to develop a software in VB.NET that read the customer address from MS SQL, and show them on
WIndows desktop application. I have no idea how to do this. THerefor I just wondering if you could help me.

Thanks in advance!
Left by Shawn on Jun 21, 2008 12:39 PM

# re: KML Creator source code
Requesting Gravatar...
I developed a software in VB.NET that convert a coordinate system between UTM and other but i need to transfer a points to google eath, so i need to idea or source code to do this

Left by Mutaz on Jul 23, 2009 3:25 PM

# re: KML Creator source code
Requesting Gravatar...
Can this Function not be called from a Private Sub?
So for when an assigned button is clicked (Export) it will create the .kml?
Left by invz on Dec 17, 2009 5:38 AM

Your comment:
 (will show your gravatar)

Copyright © Tim Hibbard | Powered by: