|
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.
|