本文介绍了得到控制器KMZ文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一些code的的:

I am trying to use some code taken here:

与给定一个GET请求KMZ文件作出回应。这是我的code:

to respond with a kmz file given a GET request. This is my code:

public void GetKMZ()
{
    this.Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment;filename=GoogleMap.kmz");
        this.Response.ContentType = "application/vnd.google-earth.kmz";

        this.Response.AppendHeader("Content-Encoding", "kmz");

    byte[] bytes = null;
    MemoryStream memStream = new MemoryStream();
    XmlTextWriter xmlTW = new XmlTextWriter(memStream,Encoding.UTF8);

    xmlTW.Formatting = Formatting.Indented;
    xmlTW.WriteStartDocument();

    xmlTW.WriteStartElement("kml");
    xmlTW.WriteAttributeString("xmlns", "http://www.opengis.net/kml/2.2");
    xmlTW.WriteStartElement("Document");
        xmlTW.WriteStartElement("Style");
        xmlTW.WriteAttributeString("id", "s1");
        xmlTW.WriteStartElement("LineStyle");
        xmlTW.WriteElementString("color", "7f0000ff");
        xmlTW.WriteElementString("width", "3");
        xmlTW.WriteEndElement();
        xmlTW.WriteEndElement();

        xmlTW.WriteElementString("name", "Chicago Transit Map");
        xmlTW.WriteElementString("description", "Chicago Transit Authority train lines");

        xmlTW.WriteStartElement("Placemark");
            xmlTW.WriteElementString("styleUrl", "#s1");
            xmlTW.WriteElementString("name", "Chicago Transit Map");
            xmlTW.WriteStartElement("LineString");
                xmlTW.WriteElementString("altitudeMode", "relative");
                xmlTW.WriteElementString("coordinates", "-87.89289951324463,41.97881025520548,0 -87.89184808731079,41.97788506340239,0 -87.89150476455688,41.97762983571196,0");

            xmlTW.WriteEndElement();
            xmlTW.WriteEndElement();

        xmlTW.WriteEndElement();

    xmlTW.WriteEndElement(); //Document

    xmlTW.WriteEndDocument(); // kml
    xmlTW.Close();

    bytes = memStream.ToArray(); // vs .GetBuffer();

    MemoryStream memStream2 = new MemoryStream();
    using (ZipOutputStream gzOs = new ZipOutputStream(memStream2))
    {
    ZipEntry entry = new ZipEntry("GoogleMap.kml");
    gzOs.SetLevel(9);
    gzOs.PutNextEntry(entry);
    gzOs.Write(bytes, 0, bytes.Length);
    gzOs.CloseEntry();
    gzOs.Close();
    }
    this.Response.Clear();
    this.Response.BinaryWrite(memStream2.ToArray());
    this.Response.End();
}

所产生的KMZ文件工作,如果我用winrar解压它作为KML文件(即我可以在谷歌地图中打开它)。不幸的是,谷歌地球/地图并不像KMZ文件GET请求产生。我使用ASP.NET MVC 3莫非压缩DLL(编译为.NET 2.0):

The produced kmz file works if I unzip it as kml file using winrar (i.e. I can open it in google map). Unfortunately, google earth/map does not like the kmz file the GET request produces. I am using ASP.NET MVC 3. Could the zip dll (compiled for .net 2.0):

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;

是一个问题?

感谢。

基督教

推荐答案

事情似乎与DotNetZip工作:

Things seem to work with DotNetZip:

// http://stackoverflow.com/questions/4717605/asp-net-vb-kml-generator
[AcceptVerbs(HttpVerbs.Get)]
public void GetKMZ()
{
    Response.AppendHeader("Connection", "close");
    Response.ContentType = "application/vnd.google-earth.kmz";
    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

    ZipOutputStream outzip = new ZipOutputStream(Response.OutputStream);
    outzip.EnableZip64 = Zip64Option.Never;
    outzip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
    outzip.PutNextEntry("doc.kml");

    XmlTextWriter xmlTW = new XmlTextWriter(outzip,Encoding.UTF8);

    xmlTW.Formatting = Formatting.Indented;
    xmlTW.WriteStartDocument();

    xmlTW.WriteStartElement("kml");
    xmlTW.WriteAttributeString("xmlns", "http://www.opengis.net/kml/2.2");
    xmlTW.WriteStartElement("Document");
        xmlTW.WriteStartElement("Style");
        xmlTW.WriteAttributeString("id", "s1");
        xmlTW.WriteStartElement("LineStyle");
        xmlTW.WriteElementString("color", "7f0000ff");
        xmlTW.WriteElementString("width", "3");
        xmlTW.WriteEndElement();
        xmlTW.WriteEndElement();

        xmlTW.WriteElementString("name", "Chicago Transit Map");
        xmlTW.WriteElementString("description", "Chicago Transit Authority train lines");

        xmlTW.WriteStartElement("Placemark");
            xmlTW.WriteElementString("styleUrl", "#s1");
            xmlTW.WriteElementString("name", "Chicago Transit Map");
            xmlTW.WriteStartElement("LineString");
                xmlTW.WriteElementString("altitudeMode", "relative");
                xmlTW.WriteElementString("coordinates", "-87.89289951324463,41.97881025520548,0 -87.89184808731079,41.97788506340239,0 -87.89150476455688,41.97762983571196,0");

            xmlTW.WriteEndElement();
            xmlTW.WriteEndElement();

        xmlTW.WriteEndElement();

    xmlTW.WriteEndElement(); //Document

    xmlTW.WriteEndDocument(); // kml

    xmlTW.Flush();
    outzip.Close();
} 

这篇关于得到控制器KMZ文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 21:05