The following code shows the guts of an ASPX page that will return a KML file that will load in Google Earth. We use an XmlTextWriter to write an XML file to the response stream. We set the ContentType = "application/vnd.google-earth.kml+xml", so it will automatically open in Google Earth if the user has it installed. The page accepts 3 query strings.
lat - latitude of geographic point
lon - longitude of geographic point
icon - url of icon that will plot the point
If query strings are not provided, it will default to Memorial Stadium in Lawrence, KS.
Like this - http://timhibbard.com/demo/kml.aspx
Here is an example of a url using the query strings that will show Arrowhead Stadium in Kansas City:
http://www.timhibbard.com/demo/kml.aspx?lat=39.0485&lon=-94.484&icon=http://timhibbard.com/images/chiefs_icon.jpg
Here is the code:
Imports System.Xml
Partial Class kml
Inherits System.Web.UI.Page
Dim lat As String = "38.963"
Dim lon As String = "-95.247"
Dim icon As String = "http://timhibbard.com/images/jayhawk.png"
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
'grab any query strings
Try
If Request.QueryString.Get("lat") <> "" Then
lat = Request.QueryString.Get("lat")
End If
If Request.QueryString.Get("lon") <> "" Then
lon = Request.QueryString.Get("lon")
End If
If Request.QueryString.Get("icon") <> "" Then
icon = Request.QueryString.Get("icon")
End If
Catch ex As Exception
End Try
BuildKML()
End Sub
Public Sub BuildKML()
My.Response.Clear()
My.Response.ContentType = "application/vnd.google-earth.kml+xml"
'uncomment following line to view raw xml
'My.Response.ContentType = "plain/text"
My.Response.ContentEncoding = System.Text.Encoding.UTF8
Dim stream As New System.IO.MemoryStream
Dim XMLwrite As New XmlTextWriter(stream, System.Text.Encoding.UTF8)
XMLwrite.WriteStartDocument()
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteStartElement("kml")
XMLwrite.WriteAttributeString("xmlns", "http://earth.google.com/kml/2.0")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteStartElement("Placemark")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("Snippet", "KML generated by TimHibbard.com")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("description", "KML Demo - read 'http://geekswithblogs.net/thibbard'>my blog")
XMLwrite.WriteElementString("name", "KML Demo")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteStartElement("LookAt")
XMLwrite.WriteElementString("longitude", lon)
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("latitude", lat)
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("range", "300")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("tilt", "20")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("heading", "0")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteEndElement()
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("visibility", "1")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteStartElement("Style")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteStartElement("IconStyle")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteStartElement("Icon")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("href", icon)
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("w", "-1")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("h", "-1")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteEndElement()
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteEndElement()
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteEndElement()
XMLwrite.WriteStartElement("Point")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("extrude", "1")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("altitudeMode", "relativeToGround")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("coordinates", lon & ", " & lat & ", 0")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteEndElement()
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteEndElement()
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteEndElement()
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteEndDocument()
XMLwrite.Flush()
Dim reader As IO.StreamReader
stream.Position = 0
reader = New IO.StreamReader(stream)
Dim bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd())
My.Response.BinaryWrite(bytes)
My.Response.End()
End Sub
End Class
|