OpenLayers 4.6.5设置功能颜色



所以我试图设置一个功能颜色,比如

addInteraction() {
this.style = new Style({
fill: new Fill({
color: this.fillColour,
}),
stroke: new Stroke({
color: this.lineColour,
width: 2
}),
image: new CircleStyle({
radius: 7,
fill: new Fill({
color: this.fillColour
}),
stroke: new Stroke({
color: this.lineColour
})
})
})
this.draw = new Draw({
source: this.vectorSource,
style: [this.style],
type: this.selectedShape,
})
this.coreMap.map.addInteraction(this.draw)
this.snap = new Snap({ source: this.vectorSource })
this.coreMap.map.addInteraction(this.snap);
this.coreMap.map.addInteraction(this.modifyLayer);
}

现在,当我绘制该功能时,假设它是一个带有红线和蓝色填充的圆圈,在我绘制它时,它将显示一个带有红色线条和蓝色填充物的圆圈,但一旦完成绘制,它将默认为浅蓝色上蓝色的openlayers默认颜色。

如果我将样式应用于vectorLayer,它将持续存在,但我希望功能保持颜色,而不是层,因为我希望在一层上有多个具有多种颜色的功能。我尝试了一些不同的方法,例如用简单的设置方法在newDraw对象外设置颜色,或者在draw对象内用样式函数设置样式,但没有成功。

您可以将样式直接分配给特性。一个很好的时间和地点是drawend听众:

draw.on('drawend', function(e) {
e.feature.setStyle(style);
});

因此,每当绘制完一个特征时,它都会得到它的样式。

最新更新