保存交互绘制openlayers

Save interaction draw openlayers

任何人都可以告诉我如何保存我在 openlayers 3 中绘制的交互图吗?我可以为此使用 json 吗?谁能提供一个简单的例子?

谢谢!

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

然后,保存到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);

加载 JSON:

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