2017-03-25 41 views
0

使用ol.format.KML writeFeatures()時,我無法理解defaultStyle。我寫出的功能沒有單獨的樣式,因此我想將圖層樣式應用於KML功能。我試過這個:KML格式writeFeatures defaultStyle

var features = vectorLayer.getSource().getFeatures(); // these do not have styles 
var layerStyle = vectorLayer.getStyleFunction()(features[0], map.getView().getResolution()); 
var kmlFormat = new ol.format.KML({ 
    defaultStyle: layerStyle 
}) 

var kml = kmlFormat.writeFeatures(features, { 
    featureProjection: map.getView().getProjection() // source coord sys 
}); 
console.log(kml); 

但是kml沒有造型。如果我圖層樣式複製到每個功能:

features.forEach(function(feature) { 
    feature.setStyle(layerStyle); 
}); 

然後KML具有造型,即使我刪除{defaultStyle: layerStyle}。但我不希望將圖層中的樣式複製到每個單獨的功能中。理想情況下,我希望kml爲沒有自己樣式的功能提供圖層樣式,並將功能樣式用於具有自己樣式的功能。

那麼defaultStyle做什麼以及什麼時候使用?

OpenLayers docs

也許defaultStyle僅適用於讀取KML,不寫?

回答

2

KML與其他功能格式不同。它被設計爲帶有樣式的功能。因此,在編寫KML時,只有與功能關聯的樣式纔會以KML結尾。這與defaultStyle設置無關,該設置僅在配置沒有爲功能配置樣式時配置用於readFeatures的樣式。