tags:

views:

2995

answers:

3

I wanted to generate (Using C#/ASP.NET2.0 on IIS) and return a KML document directly to the browser... without writing a temporary file to the server or relying on a 3rd party library or class.

Here's how I did it:

Server

  1. Add the .kml mimetype to the folder where you want this "file" to live. Say, \\myDevServer\...\InetPub\KML
    (Google's instructions are only for Apache)
    1. Open Internet Information Services (IIS) Manager on your DEV server
    2. Navigate to your DEV site
    3. Right-click the KML folder and choose Properties
    4. Click the HTTP Headers tab
    5. Click the MIME types button
    6. Click New
    7. Enter
      • Extension: .kml
      • MIME Type: application/vnd.google-earth.kml+xml
    8. Click OK twice to get back to the HTTP Headers tab
  2. Set the KML folder as an ASP.NET application (maybe optional depending on how your server is set up)
    1. Click the Directory tab
    2. Click the Create button
    3. The Application name field becomes active with the setting KML
    4. Click OK taking you back to the main IIS Manager window

Website

  1. Open VS2008:
    1. File >> New Website
    2. Choose:
      • Empty Web Site
      • Language: C#
      • Location: \\myDevServer\...\InetPub\KML\
  2. In Solution Explorer
    1. Rightclick the website
    2. Choose New Item
    3. Choose Generic Handler from the Visual Studio installed templates window
    4. Enter a name (I used MelroseVista.ashx )
    5. Choose Language: Visual C#
    6. Click OK
  3. Paste the following code

//

using System;
using System.Web;
using System.Xml;

public class Handler : IHttpHandler
{
    public void ProcessRequest( HttpContext context)
    {
     context.Response.ContentType = "application/vnd.google-earth.kml+xml";
     context.Response.AddHeader("Content-Disposition", "attachment; filename=MelroseVista.kml");

     XmlTextWriter kml = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8);

     kml.Formatting = Formatting.Indented;
     kml.Indentation = 3;

     kml.WriteStartDocument();

     kml.WriteStartElement("kml", "http://www.opengis.net/kml/2.2");
     kml.WriteStartElement("Placemark");
     kml.WriteElementString("name", "Melrose Vista   FL");
     kml.WriteElementString("description", "A nice little town");

     kml.WriteStartElement("Point");

     kml.WriteElementString("coordinates", "-80.18451400000000000000,26.08816400000000000000,0");

     kml.WriteEndElement(); // <Point>
     kml.WriteEndElement(); // <Placemark>
     kml.WriteEndDocument(); // <kml>

     kml.Close();

    }
    public bool IsReusable
    {
     get
     {
      return false;
     }
    }
}
  1. Attempt to load your page in your favorite browser
  2. You should get a popup asking you to open or save the resulting KML file.
  3. If you open it, you should have GoogleEarth launch itself and zoom to a thumbtack in Eastern Florida
  4. If you save it, you should see the following in the file

\

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"&gt;
   <Placemark>
      <name>Melrose Vista   FL</name>
      <description>A nice little town</description>
      <Point>
         <coordinates>-80.18451400000000000000,26.08816400000000000000,0</coordinates>
      </Point>
   </Placemark>
</kml>

Final thoughts

  1. Obviously(?) this is just a baby-toy prototype.
  2. XmlTextWriter worked pretty well here. However, I think XMLDocument looks more promising for larger KML files since you can manipulate it in memory before pushing it to the user. If, for example, you want the same point to appear in multiple folders in the GoogleEarth Locations tree.

Props to Mehrdad for pointing me in the right direction. Your huge rep is well deserved.

(The markdown processor really doesn't seem to like code blocks contained in bulleted lists. In the end I had to add an extra line between the bullet and the start of the code block. Of course, that screws up the bullet numbering. But I'm out of time so I'm going to leave it)

A: 

I would recommend using a Generic Handler rather than a Page

harriyott
+3  A: 

I suggest you consider using an HTTP Handler instead of a ASP.NET page. It will be cleaner and more performant. Just add new item of type "Generic Handler" to your project and consider moving the code to its ProcessRequest method. The general approach is good, though.

By the way, unless you are explicitly mapping .kml files to an ASP.NET handler, it'll not run anyway. I suggest going with the default .ashx extension and add a Content-Disposition HTTP header to set the filename for the client:

Response.AddHeader("Content-Disposition", "attachment; filename=File.kml");

Also, note that you should set header stuff before anything is sent to the client so you should move setting Content-Type and adding header before other stuff.

Mehrdad Afshari
I'll look into using an HTTP handler thanks. Do you have a favorite, specific example?Assuming I end up going with what I have, would you point me towards an answer #2 above? My background is mostly perl hacking and I'm having trouble getting my head around integrating the code-behind to the actual page the browser's asking for.
Sukotto
hmm... comments don't preserve vertical whitespace :-(
Sukotto
You don't need an example. Just right click on the project -> Add new item -> Generic Handler. It'll create a template for you. Copy and paste the code in Page_Load above to the ProcessRequest method. The only thing you need to change is adding a "var Response = context.Response" at the beginning of the method. I found one anyway: http://stackoverflow.com/questions/873207/force-download-of-a-file-on-web-server-asp-net-c/873228#873228
Mehrdad Afshari
A: 

heaaaaaaaaaaaay.. I need to add my KML file to a googlemap.. and I don't know how to do it with ASHX files.. can you explain with more details !!!

aya