2011-01-17 7 views
2

다음은 Ionic 압축 클래스 (Ionic = Codeplex의 DotNetZip)를 사용하지 않고 KML을 생성 할 수있는 KMZ 파일을 생성하는 처리기 (ashx) 파일입니다.이 핸들러는 SQL 테이블을 사용하여 작업 공간 마크 그러나 ASHX 처리기 사용, 사용자 정의 아이콘 사용, 파일을 제대로 압축 및 장소 표시를 채우는. context.Response.OutputStream을 사용의 좋은 예입니다, 이것은 .NET 엔진 내에서 매우 효율적입니다.ASP.NET VB KML 생성기

<%@ 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 

답변

0

위의 예는 질문이 아니라 VB.net과 ASP.NET을 사용하여 kml/kmz 파일을 만드는 데 도움이되기를 바랍니다.

0

다른 시험이 있습니다. ple 엄격히 사용 SharpKml.

public void ProcessRequest(HttpContext context) 
{ 

    context.Response.AppendHeader("Connection", "close"); 
    context.Response.ContentType = "application/vnd.google-earth.kmz"; 
    context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); 
    context.Response.AddHeader("Content-Disposition", "attachment; filename=mykmz.kmz"); 

    Point point = new Point(); 
    point.Coordinate = new Vector(-13.163959, -72.545992); 

    Placemark placemark = new Placemark(); 
    placemark.Geometry = point; 
    placemark.Name = "Machu Picchu"; 

    KmlFile kml = KmlFile.Create(placemark, false); 

    KmzFile kmz = KmzFile.Create(kml); 
    kmz.Save(context.Response.OutputStream); 

}