Is it possible to write and save a KML from OpenLayers? Anyone know of an example of exporting one?
IF you are using Openlayers 3 or 4, you will find that the syntax of previous (2012) answers does not work anymore.
This does:
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;
}