在一层上打开不同颜色的形状?

Openlayers different colour shapes on one layer?

所以我编写了允许我向地图添加形状的代码,绘制函数如下所示

  addInteraction() {
    this.coreMap.map.addInteraction(this.modifyLayer);
    this.draw = new Draw({
      source: this.vectorSource,
      style: this.style,
      type: 'Point'
    });
    this.coreMap.map.addInteraction(this.draw);
    this.snap = new Snap({source: this.vectorSource});
    this.coreMap.map.addInteraction(this.snap);
  }

让我在地图上绘制效果很好,但是如果我尝试将 vectorLayer 上的样式更改为从绿色点变为红色点,它会将每个点都更改为红色而不是新点,我已经尝试了几件事;

目前我假设每次我想创建一个具有不同颜色的新形状时我都必须创建一个新层,如果没有办法绕过它也没关系,但我更喜欢它留在一层内。

您可以为每个新创建的要素设置样式,或者为每个要素设置颜色 属性,例如 feature.set('color', 'red'); 并为图层使用样式函数。这将使用功能上设置的颜色,如果设置了 none

,则默认为黑色
style: function(feature) {
  return new Style({

    ...

      color: feature.get('color') || 'black',

   ...

  })
}