ASP.NET VB KML生成器

ASP.NET VB KML生成器,asp.net,vb.net,kml,ashx,kmz,Asp.net,Vb.net,Kml,Ashx,Kmz,下面是一个生成KMZ文件的处理程序(ashx)文件(可以通过不使用Ionic压缩类(Ionic=DotNetZip at Codeplex)来生成KML)。此处理程序使用SQL表填充位置标记,但这是ashx处理程序使用、自定义图标使用、正确压缩文件和填充位置标记的一个很好的示例。通过使用context.Response.OutputStream,这在.NET引擎中非常有效 <%@ WebHandler Language="VB" Class="YourKML" %> Imports

下面是一个生成KMZ文件的处理程序(ashx)文件(可以通过不使用Ionic压缩类(Ionic=DotNetZip at Codeplex)来生成KML)。此处理程序使用SQL表填充位置标记,但这是ashx处理程序使用、自定义图标使用、正确压缩文件和填充位置标记的一个很好的示例。通过使用context.Response.OutputStream,这在.NET引擎中非常有效

<%@ WebHandler Language="VB" Class="YourKML" %>

Imports System
Imports System.Web
Imports System.Web.Configuration
Imports System.Xml
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.SqlServer.Types
Imports MySql.Data.MySqlClient
Imports Ionic.Zip

Public Class YourKML : Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.AppendHeader("Connection", "close")
        'Response.ContentType = "application/vnd.google-earth.kml+xml"
        'Response.ContentEncoding = System.Text.Encoding.UTF8
        context.Response.ContentType = "application/vnd.google-earth.kmz"
        context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache)

        Dim outzip As New ZipOutputStream(context.Response.OutputStream)
        outzip.EnableZip64 = Zip64Option.Never
        outzip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression
        outzip.PutNextEntry("doc.kml")

        Dim kmlout As New XmlTextWriter(outzip, System.Text.Encoding.UTF8)
        kmlout.WriteStartDocument()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("kml")
        kmlout.WriteAttributeString("xmlns", "http://www.opengis.net/kml/2.2")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("Document")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteElementString("name", "doc.kml")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("Style")
        kmlout.WriteAttributeString("id", "yoursym")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("IconStyle")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("Icon")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteElementString("href", "http://youriconimage")
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteStartElement("hotSpot")
        kmlout.WriteAttributeString("x", "0.5")
        kmlout.WriteAttributeString("y", "0.5")
        kmlout.WriteAttributeString("xunits", "fraction")
        kmlout.WriteAttributeString("yunits", "fraction")
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        PlacesKML(kmlout)
        kmlout.WriteEndElement()
        kmlout.WriteWhitespace(vbCrLf)
        kmlout.WriteEndDocument()
        kmlout.Flush()
        outzip.Close()
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property

    Sub PlacesKML(ByVal kmlout As XmlTextWriter)
        Using connection As New SqlConnection(WebConfigurationManager.ConnectionStrings("YourConnectionString").ConnectionString)
            connection.Open()
            Dim cmd As New SqlCommand("SELECT yourkey, Location FROM yourtable", connection)
            Dim positrs As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            Dim curkey As String = ""
            Dim comment As StringBuilder = New StringBuilder()
            Dim loc As New SqlGeography()
            With positrs
                While .Read()
                    Dim yourkey As String = .GetString(.GetOrdinal("yourkey"))
                    If String.IsNullOrEmpty(yourkey) Then Continue While
                    If yourkey <> curkey Then
                        If Not String.IsNullOrEmpty(curkey) Then
                            kmlout.WriteStartElement("Placemark")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteElementString("name", curkey)
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteStartElement("description")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteCData(comment.ToString())
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteEndElement() ' End Description
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteElementString("styleUrl", "#yoursym")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteStartElement("Point")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteElementString("coordinates", loc.Long.ToString() & ", " & loc.Lat.ToString() & ", 0")
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteEndElement() ' End Point
                            kmlout.WriteWhitespace(vbCrLf)
                            kmlout.WriteEndElement() ' End Place Mark
                            kmlout.WriteWhitespace(vbCrLf)
                        End If
                        curkey = yourkey
                        comment.Length = 0
                        comment.Append(yourkey)
                        loc = .GetProviderSpecificValue(.GetOrdinal("Location"))
                    Else
                        comment.Append("<br/>").Append(yourkey)
                    End If
                End While
                If Not String.IsNullOrEmpty(curkey) Then
                    kmlout.WriteStartElement("Placemark")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteElementString("name", curkey)
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteStartElement("description")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteCData(comment.ToString())
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteEndElement() ' End Description
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteElementString("styleUrl", "#yoursym")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteStartElement("Point")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteElementString("coordinates", loc.Long.ToString() & ", " & loc.Lat.ToString() & ", 0")
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteEndElement() ' End Point
                    kmlout.WriteWhitespace(vbCrLf)
                    kmlout.WriteEndElement() ' End Place Mark
                    kmlout.WriteWhitespace(vbCrLf)
                End If
            End With
            positrs.Close()
        End Using
    End Sub
End Class

导入系统
导入系统.Web
导入System.Web.Configuration
导入System.Xml
导入系统数据
导入System.Data.SqlClient
导入Microsoft.SqlServer.Types
导入MySql.Data.MySqlClient
导入爱奥尼亚
公共类YourKML:实现IHttpHandler
Public Sub-ProcessRequest(ByVal上下文作为HttpContext)实现IHttpHandler.ProcessRequest
context.Response.AppendHeader(“连接”,“关闭”)
'Response.ContentType=“application/vnd.google earth.kml+xml”
'Response.ContentEncoding=System.Text.Encoding.UTF8
context.Response.ContentType=“应用程序/vnd.google earth.kmz”
context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache)
Dim outzip作为新的ZipoutStream(context.Response.OutputStream)
outzip.EnableZip64=Zip64选项。从不
outzip.CompressionLevel=Ionic.Zlib.CompressionLevel.BestCompression
输出邮政编码(“文件kml”)
Dim kmlout作为新的XmlTextWriter(outzip,System.Text.Encoding.UTF8)
kmlout.WriteStartDocument()文件
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteStartElement(“kml”)
kmlout.WriteAttributeString(“xmlns”)http://www.opengis.net/kml/2.2")
kmlout.WriteWhitespace(vbCrLf)
K.书面声明(“文件”)
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteElementString(“名称”、“doc.kml”)
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteStarteElement(“样式”)
kmlout.WriteAttributeString(“id”、“yoursym”)
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteStarteElement(“IconStyle”)
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteStarteElement(“图标”)
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteElementString(“href”http://youriconimage")
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteEndElement()
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteStarteElement(“热点”)
kmlout.WriteAttributeString(“x”,“0.5”)
kmlout.WriteAttributeString(“y”、“0.5”)
kmlout.WriteAttributeString(“xunits”、“分数”)
kmlout.WriteAttributeString(“yunits”,“分数”)
kmlout.WriteEndElement()
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteEndElement()
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteEndElement()
kmlout.WriteWhitespace(vbCrLf)
PlacesKML(公里)
kmlout.WriteEndElement()
kmlout.WriteWhitespace(vbCrLf)
Kmloot.WriteEndDocument()
kmlout.Flush()
outzip.Close()
端接头
公共只读属性IsReusable()作为布尔值实现IHttpHandler.IsReusable
得到
返回真值
结束
端属性
子位置SKML(ByVal kmlout作为XmlTextWriter)
将连接用作新的SqlConnection(WebConfiguration Manager.ConnectionString(“YourConnectionString”).ConnectionString)
connection.Open()
Dim cmd作为新的SqlCommand(“选择您的键,从您的表中选择位置”,连接)
作为SqlDataReader=cmd.ExecuteReader(CommandBehavior.CloseConnection)的Dim positers
Dim curkey As String=“”
标注注释为StringBuilder=New StringBuilder()
Dim loc作为新的SqlGeography()
与假定
While.Read()
将您的密钥设置为字符串=.GetString(.GetOrdinal(“您的密钥”))
如果String.IsNullOrEmpty(您的密钥),则在
如果你的钥匙是钥匙那么
如果不是String.IsNullOrEmpty(curkey),则
kmlout.WriteStarteElement(“Placemark”)
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteElementString(“名称”,curkey)
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteStarteElement(“说明”)
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteCData(comment.ToString())
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteEndElement()'结束说明
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteElementString(“styleUrl”,“yoursym”)
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteStartElement(“点”)
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteElementString(“坐标”,loc.Long.ToString()&“,”和loc.Lat.ToString()&”,0”)
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteEndElement()'端点
kmlout.WriteWhitespace(vbCrLf)
kmlout.WriteEndElement()'结束位置标记
kmlout.WriteWhitespace(vbCrLf)
如果结束
curkey=yourkey
comment.Length=0
comment.Append(yourkey)
loc=.GetProviderSpecificValue(.GetOrdinal(“位置”))
其他的
comment.Append(“
”).Append(您的键) 如果结束 结束时 如果不是String.IsNullOrEmpty(curkey),则 kmlout.WriteStarteElement(“Placemark”) kmlout.WriteWhitespace(vbCrLf) kmlout.WriteElementString(“名称”,curkey)