2016-03-10 69 views
0

任何人都可以給我一個提示,我如何保存我在openlayers 3中繪製的交互繪圖?我可以使用json嗎?任何人都可以提供一個簡單的例子?保存交互繪製開放層

謝謝!

回答

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

感謝非常有幫助!你能不能提供一個關於如何從這個文件中加載數據的代碼示例? – Panos

+0

我編輯了帖子!請將答案標記爲已接受,如果這解決了您的問題:) – kaycee