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
- 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)- Open
Internet Information Services (IIS) Manager
on your DEV server - Navigate to your DEV site
- Right-click the
KML
folder and chooseProperties
- Click the
HTTP Headers
tab - Click the
MIME types
button - Click
New
- Enter
- Extension: .kml
- MIME Type: application/vnd.google-earth.kml+xml
- Click
OK
twice to get back to theHTTP Headers
tab
- Open
- Set the
KML
folder as an ASP.NET application (maybe optional depending on how your server is set up)- Click the
Directory
tab - Click the
Create
button - The
Application name
field becomes active with the settingKML
- Click
OK
taking you back to the main IIS Manager window
- Click the
Website
- Open VS2008:
- File >> New Website
- Choose:
Empty Web Site
- Language:
C#
- Location:
\\myDevServer\...\InetPub\KML\
- In
Solution Explorer
- Rightclick the website
- Choose
New Item
- Choose
Generic Handler
from theVisual Studio installed templates
window - Enter a name (I used
MelroseVista.ashx
) - Choose Language:
Visual C#
- Click
OK
- 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;
}
}
}
- Attempt to load your page in your favorite browser
- You should get a popup asking you to
open
orsave
the resulting KML file. - If you
open
it, you should have GoogleEarth launch itself and zoom to a thumbtack in Eastern Florida - 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">
<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
- Obviously(?) this is just a baby-toy prototype.
XmlTextWriter
worked pretty well here. However, I thinkXMLDocument
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)