2016-02-25 54 views
1

我正在使用OpenLayers版本:v3.13.0,我試圖導出我的圖層中的所有fetaures。我的代碼如下Openlayers 3 KML導出writeFeatures()不起作用

var projection = ol.proj.get('EPSG:3857'); 
 
    var format = new ol.format.KML({ 
 
     'maxDepth': 10, 
 
     'extractStyles': true, 
 
     'internalProjection': projection, 
 
     'externalProjection': projection 
 
    }); 
 
var newfeatures = []; 
 
var vectorSource = layer.getSource(); 
 
vectorSource.forEachFeature(function(feature) { 
 
     var clone = feature.clone(); 
 
     clone.setId(feature.getId()); // clone does not set the id 
 
     clone.getGeometry().transform(projection, 'EPSG:4326'); 
 
     newfeatures.push(clone); 
 
}); 
 
//console.log(newfeatures); 
 
var string = new ol.format.KML().writeFeatures(newfeatures); 
 
//console.log(string);

我收到錯誤 「遺漏的類型錯誤:未定義無法讀取屬性‘長’」

當我安慰變量newfeatures我得到的所有在數組中繪製特徵。請幫助我解決這個問題

+0

我修改writeFeatures(newfeatures); writeFeatures(newfeatures [0]);然後錯誤消失,但功能不會轉換爲xml輸出,如下所示 user2399505

回答

2

您可以導出這些功能,而無需手動克隆和轉換它們。替換上面

var features = layer.getSource().getFeatures(); 
var string = new format.KML().writeFeatures(features, { 
    featureProjection: map.getView().getProjection() 
}); 

上面的代碼假定map變量保存您的ol.Map比如你的整個代碼。

請注意,在ol.format.KML上沒有maxDepth,internalProjectionexternalProjection選項。

+0

感謝您的回覆....但仍然得到同樣的錯誤...遺漏的類型錯誤:無法讀取未定義ol.js的特性‘長度’:601 時我控制var功能獲取功能對象作爲數組 – user2399505

+0

我建議你暫時使用'ol-debug.js'而不是'ol.js',那麼你可以更容易地調試並獲得適當的堆棧跟蹤在開發控制檯。 – ahocevar

1

寫設有使用3857投影

function GetKMLFromFeatures(features) { 
    var format = new ol.format.KML(); 
    var kml = format.writeFeatures(features, {featureProjection: 'EPSG:3857'}); 
    return kml; 
}