保存交互绘制openlayers

时间:2016-03-10 12:59:20

标签: javascript save openlayers-3

有人可以给我一个提示,如何保存我在openlayers 3中绘制的交互图纸?我可以用json吗?任何人都能提供一个简单的例子吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

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