openlayer4:如何删除图层中的要素并刷新地图

时间:2018-05-15 06:10:52

标签: javascript openlayers layer

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

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

1 个答案:

答案 0 :(得分: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));

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