È possibile scrivere e salvare un KML da OpenLayers? Qualcuno sa di un esempio di esportarne uno?Gli Openlayer scrivono e salvano un KML in base alla mappa
risposta
È 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();
Nice. Come potresti scrivere o salvare il KML? – user1040259
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
Grazie per il vostro aiuto. Forza il browser ad avviare il download del KML. – user1040259
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>');
});
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;
}
- 1. Openlayer zIndex per marcatori e vettori
- 2. Aggiunta del layer Kml alla mappa di google
- 3. puoi usare nokia qui API mappa REST in openlayer?
- 4. Gli Openlayer richiamano la funzionalità CSS popup
- 5. Visualizzazione di nomi sulla mappa utilizzando KML
- 6. Tabella Fusion Importazione KML
- 7. Come si salvano gli input IRB in un file .rb?
- 8. QML console.log() e console.debug() non scrivono alla console
- 9. Molte e molte relazioni non salvano
- 10. Come visualizzare i dati utilizzando gli openlayer con OpenStreetMap in geodjango?
- 11. Android: come caricare KML
- 12. JXL e Timezone che scrivono un Excel
- 13. Come reindirizzare gli URL in base alla stringa di query?
- 14. Lettura e analisi KML in java
- 15. jQuery riordina gli elementi dell'elenco in base alla classe
- 16. E 'la resa' in Scala equivalente alla funzione mappa?
- 17. Rileva valuta in base alla posizione dell'utente?
- 18. Elixir - Looping e aggiunta alla mappa
- 19. conversione a livello di codice kml nell'immagine
- 20. Grails - Aggiungere un parametro alla mappa params
- 21. ehcache in base alla data
- 22. Come disegnare un tracciato su una mappa usando il file kml?
- 23. filtraggio in base alla data in elasticsearch
- 24. == e% in% differiscono in base alla codifica dei caratteri?
- 25. Come creare una mappa di base riutilizzabile
- 26. Ruby: libreria KML?
- 27. Modifica orientamento indicatore mappa Google in base alla direzione del percorso
- 28. colorize single track in file kml
- 29. Clojure che estrae il valore dalla mappa in base alla logica prioritaria
- 30. Cambia dimensione carattere in base alla lingua
Ecco una soluzione simile: potrebbe aiutare? http://gis.stackexchange.com/questions/17031/openlayers-format-kml-write- style – user1040259