Geeks With Blogs

News



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="http://earth.google.com/kml/2.0">
  <Placemark>
    <Snippet>Tim Hibbard's real time location</Snippet>
    <description>Location of Tim Hibbard, Software Architect for EnGraph</description>
    <name>Tim Hibbard</name>
    <LookAt>
      <longitude>Replace Me later</longitude>
      <latitude>Replace Me later</latitude>
      <range>3000</range>
      <tilt>60</tilt>
      <heading>0</heading>
    </LookAt>
    <visibility>1</visibility>
    <Style>
      <IconStyle>
        <Icon>
          <href>http://www.timhibbard.com/images/mesmall.jpg(enter your own url here)</href>
          <w>-1</w>
          <h>-1</h>
        </Icon>
     </IconStyle>
   </Style>
   <Point>
     <extrude>1</extrude>
     <altitudeMode>relativeToGround</altitudeMode>
     <coordinates>replace with lat,replace with lon,0</coordinates>
   </Point>
 </Placemark>
</kml>


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
   xDoc.Load("http://www.timhibbard.com/kml/base.xml")

    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
   xDoc.Save(tmpFileName)
   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 "http://www.timhibbard.com/kml/" & 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!
Shawn
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



Thanx
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: GeeksWithBlogs.net