代码之家  ›  专栏  ›  技术社区  ›  Munerz

OpenLayers 4.6.5设置特征颜色

  •  0
  • Munerz  · 技术社区  · 6 年前

    所以我试着设置一个这样的特征颜色,

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

    如果我将样式应用到vectorLayer,它将保持不变,但我希望特性保持颜色而不是图层,因为我希望在一个图层上具有多种颜色的多个特性,我尝试了几种不同的方法,例如使用简单的设置方法在newDraw对象外部设置颜色,或者在draw对象中使用样式函数设置样式,但运气不佳。

    1 回复  |  直到 6 年前
        1
  •  0
  •   ahocevar    6 年前

    可以将样式直接指定给要素。一个好的时间和地点,这样做将是一个很好的选择 drawend 侦听器:

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