0

So I'm trying to set a feature colour like so,

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

Now when I'm drawing the feature let's say it's a circle with red line and blue fill it will show a circle with a red line and blue fill while I'm drawing it but once it's completed drawing it will default to the openlayers default colours of the blue on light blue.

If I apply the style to the vectorLayer it will persist but I want the feature to hold the colour not the layer as I want multiple features with multiple colours on one layer, I've tried a couple of different things such as setting the colour outside of the newDraw object with a simple set method, or setting the style with a style function inside the draw object with no luck.

Munerz
  • 1,052
  • 1
  • 13
  • 26

1 Answers1

0

You can assign a style directly to a feature. A good time and place to do that would be a drawend listener:

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

So whenever you finish drawing a feature, it gets its style.

ahocevar
  • 5,448
  • 15
  • 29