KML格式writeFeatures defaultStyle

时间:2017-03-25 17:51:57

标签: openlayers

使用ol.format.KML writeFeatures()时,我无法理解defaultStyle。我写的功能没有单独的样式,所以我想将图层样式应用于KML功能。我试过这个:

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

然后,即使我删除了{defaultStyle: layerStyle},kml仍具有样式。但我不希望将图层中的样式复制到每个单独的功能中。理想情况下,我希望kml具有不具有自己风格的功能的图层样式,并将功能的样式用于具有自己风格的功能。

那么defaultStyle做什么以及何时使用?

OpenLayers docs

也许defaultStyle仅适用于阅读KML,而不是写作?

1 个答案:

答案 0 :(得分:2)

KML与其他功能格式不同。它旨在携带风格的功能。因此,在编写KML时,只有与功能相关联的样式才会出现在KML中。这与defaultStyle设置无关,该设置仅在没有为要素配置样式时配置用于readFeatures的样式。