2012-02-20 7 views

risposta

9

È possibile esportare solo le funzioni vettoriali a KML.

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); 
} 

UPDATE

Al fine di forzare il browser a scaricare la stringa KML come file KML è necessario inviare la stringa di nuovo al lato server in modo che possa essere restituito al browser come file da scaricare.

Non hai specificato quale lingua/piattaforma/etc stai usando sul lato server Ma questo è quello che ho fatto in C#.

Ho creato un gestore che accetta un nome file da querystring e KML da un modulo area di testo.

KMLDownload.ashx:

<%@ WebHandler Language="C#" Class="KMLDownload" %> 

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; 
     } 
    } 

} 

Poi dal mio lato javascript ho semplicemente chiamare questo per avviare il download:

var filename = "NameofKMLfileI_WANT.kml"; 

var url = "secure/KMLDownload.ashx"; 
if (filename) { 
    url += "?filename=" + filename; 
} 

var input = '<TEXTAREA name="kml">' + kml + '</TEXTAREA>'; 

//send request 
jQuery('<form action="' + url + '" method="post">' + input + '</form>').appendTo('body').submit().remove(); 
+0

Nice. Come potresti scrivere o salvare il KML? – user1040259

+0

Sii specifico. Vuoi salvare nel database, scrivere su file o forzare il browser ad avviare il download di un file KML dalla stringa KML restituisce la funzione precedente? – capdragon

+0

Grazie per il vostro aiuto. Forza il browser ad avviare il download del KML. – user1040259

3

Ecco qualche azione JQuery per risparmiare:

$('#saveKML').click(function() { 
var kmlFormat = new OpenLayers.Format.KML(); 
var newWindow = window.open('', 
    'KML Export ' + (new Date()).getTime(), "width=300,height=300"); 
    newWindow.document.write('<textarea id="kml" style="width: 100%; height: 100%">' + 
    kmlFormat.write(features) + '</textarea>'); 
}); 
0

se si utilizza Openlayers 3 o 4, troverete che la sintassi delle risposte precedenti (2012) non funziona più.

Questo fa:

 function GetKMLFromFeatures(features) { 
      var format = new ol.format.KML(); 
      var kml = format.writeFeatures(features, {featureProjection: 'EPSG:3857'}); 
      return kml; 
     } 
     function GetGeoJSONFromFeatures(features) { 
      var format = new ol.format.GeoJSON(); 
      var geoJSON = format.writeFeatures(features, {featureProjection: 'EPSG:3857'}); 
      return geoJSON; 
     } 
     function GetFeaturesFromLayer(layer) { 
      var source = layer.getSource(); 
      var features = source.getFeatures(); 
      return features; 
     } 
Problemi correlati