保存交互绘制开放图层

Save interaction draw openlayers

本文关键字:图层 绘制 交互 保存      更新时间:2023-09-26

谁能给我一个提示,告诉我如何保存我在 openlayer 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'
  })
});