Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can export only the vector features to KML.</p> <pre class="lang-js prettyprint-override"><code>function GetKMLFromFeatures(features) { var format = new OpenLayers.Format.KML({ 'maxDepth':10, 'extractStyles':true, 'internalProjection': map.baseLayer.projection, 'externalProjection': new OpenLayers.Projection("EPSG:4326") }); return format.write(features); } </code></pre> <hr> <h1>UPDATE</h1> <p>In order to force the browser to download the KML string as a KML file you need to send that string back to the server-side so it can be returned to the browser as a file to download. </p> <p>You haven't specified what language/platform/etc you are using on the server-side But this is what i did in C#.</p> <p>I created a handler which takes in a the filename from the querystring and the KML from a textarea form.</p> <p><strong>KMLDownload.ashx:</strong></p> <pre class="lang-js prettyprint-override"><code>&lt;%@ WebHandler Language="C#" Class="KMLDownload" %&gt; using System; using System.Web; public class KMLDownload : IHttpHandler { public void ProcessRequest(HttpContext context) { HttpResponse response = context.Response; string kml = context.Request["kml"]; string filename = context.Request.QueryString["filename"]; if (String.IsNullOrEmpty(kml)) { context.Response.ContentType = "text/plain"; context.Response.Write("{\"error\":\"No files recevied\"}"); } else { if (String.IsNullOrEmpty(filename)){ filename = "Features_KML.kml"; } // force a download of the kml file. response.Clear(); response.ContentType = "application/kml"; response.AddHeader("Content-Disposition", "attachment; filename=" + filename); response.AddHeader("content-legth", kml.Length.ToString()); response.Write(kml.ToString()); response.End(); } } public bool IsReusable { get { return false; } } } </code></pre> <p>Then from my javascript side i simply call this to initiate the download:</p> <pre class="lang-js prettyprint-override"><code>var filename = "NameofKMLfileI_WANT.kml"; var url = "secure/KMLDownload.ashx"; if (filename) { url += "?filename=" + filename; } var input = '&lt;TEXTAREA name="kml"&gt;' + kml + '&lt;/TEXTAREA&gt;'; //send request jQuery('&lt;form action="' + url + '" method="post"&gt;' + input + '&lt;/form&gt;').appendTo('body').submit().remove(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload