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



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

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

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

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

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

最新更新