【问题标题】:openlayer4: how to delete a feature in a layer and refresh the mapopenlayer4:如何删除图层中的要素并刷新地图
【发布时间】:2018-05-15 06:10:51
【问题描述】:

我想添加一个双击事件来删除一个功能。虽然这段代码运行良好,但存在一个问题:地图无法刷新,地图中的要素仍在,直到我双击下一步。我该怎么办?

_createDoubleSelectControl: function() {
    var selectedStyle = [
        new OpenLayers.style.Style({
            stroke: new OpenLayers.style.Stroke({
                color: '#3399CC',
                width: 1.25
            })
        })
    ];
    this.doubleClickSelector = new OpenLayers.interaction.Select({
        condition: OpenLayers.events.condition.doubleClick,
        style: selectedStyle,
        multi: true,
        layers: [this.layers.cellLayer],
        filter: function(feature, layer) {
            return layer === this.layers.cellLayer;
        }.bind(this)
    });

    this.map.addInteraction(this.doubleClickSelector);
    this.doubleClickSelector.on('select', function(event) {
        let features = event.target.getFeatures();
        features.forEach(function(feature) {
            this._cellLayer.getSource().removeFeature(feature);
            //this._cellLayer.refresh();
            this._cellLayer.refresh({
                force: true,
                active: true
            });
            this.map.removeLayer(this._cellLayer);
            this.map.addLayer(this._cellLayer);
        }.bind(this));
    }.bind(this));
},

【问题讨论】:

    标签: javascript openlayers layer


    【解决方案1】:

    您必须在选择时从选择中删除该功能:

    this.doubleClickSelector.on('select',function(event){
      let features =event.target.getFeatures();
      // Clear selection
      this.doubleClickSelector.getFeatures().clear();
      // Remove from layer
      features.forEach(function(feature){
         this._cellLayer.getSource().removeFeature(feature);
      }.bind(this));
    }.bind(this));
    

    否则该功能仍处于选中状态且可见...

    【讨论】:

      猜你喜欢
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      • 2019-01-23
      • 1970-01-01
      相关资源
      最近更新 更多