I am trying to use some code taken here:
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();
}
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;
be a problem?
Thanks.
Christian