user2983190
user2983190

Reputation:

Save interaction draw openlayers

Can anybody give me a hint on how can I save the interaction drawings which I draw in openlayers 3? Can I use json for that? Can anybody provide a simple example?

Thanks!

Upvotes: 1

Views: 2047

Answers (1)

kaycee
kaycee

Reputation: 911

var features = yourLayer.getSource().getFeatures();
var newForm = new ol.format.GeoJSON();
var featColl = newForm.writeFeaturesObject(features);

Then, save it to JSON:

function exportJson(featuresCollection) {
    var txtArray = [];
    txtArray.push(JSON.stringify(featuresCollection));

// Here I use the saveAs library to export the JSON as *.txt file

    var blob = new Blob(txtArray, {type: 'text/json;charset=utf8'});
    saveAs(blob, layerName + ".txt")
};

exportJson(featColl);

To load a JSON:

var vectorLayer = new ol.layer.Vector({
  source: new ol.source.GeoJSON({
    projection: 'EPSG:3857',
    url: 'yourFile.json'
  })
});

Upvotes: 1

Related Questions