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) Manageron your DEV server - Navigate to your DEV site
- Right-click the
KMLfolder and chooseProperties - Click the
HTTP Headerstab - Click the
MIME typesbutton - Click
New - Enter
- Extension: .kml
- MIME Type: application/vnd.google-earth.kml+xml
- Click
OKtwice to get back to theHTTP Headerstab
- Open
- Set the
KMLfolder as an ASP.NET application (maybe optional depending on how your server is set up)- Click the
Directorytab - Click the
Createbutton - The
Application namefield becomes active with the settingKML - Click
OKtaking 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 Handlerfrom theVisual Studio installed templateswindow - 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
openorsavethe resulting KML file. - If you
openit, you should have GoogleEarth launch itself and zoom to a thumbtack in Eastern Florida - If you
saveit, 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.
XmlTextWriterworked pretty well here. However, I thinkXMLDocumentlooks 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)