Javascript 无法删除OpenLayers 3中的功能

Javascript 无法删除OpenLayers 3中的功能,javascript,openlayers-3,Javascript,Openlayers 3,我在stackoverflow上读过几十篇文章,但没有一篇有用。这就是我想做的: features.forEach(function(feature){ source.removeFeature(feature); console.log("removed"); console.log(feature); }); 因此,当我只选择了一项功能时,我会在控制台中看到以下消息: removed Controller.js:525:8 Object { disposed_:

我在stackoverflow上读过几十篇文章,但没有一篇有用。这就是我想做的:

features.forEach(function(feature){
    source.removeFeature(feature); 
    console.log("removed");
    console.log(feature);
});
因此,当我只选择了一项功能时,我会在控制台中看到以下消息:

removed Controller.js:525:8
Object { disposed_: false, onDisposeCallbacks_: undefined, ...}
就我在控制台中看到的而言,一切看起来都很好。但问题是该特征没有从地图上删除

编辑

现在更有趣了。如果我使用getArray将要素转换为array并执行以下操作:

 for(var i=0,len=features.length;i<len;i++){
    var feature = features[i];
    source.removeFeature(feature);
 }
 source.clear();

for(var i=0,len=features.length;i我有这个问题很久了,我想不出来。事实证明,这似乎是OpenLayers中的刷新问题。我发现让层刷新的方法是使其不可见,然后再次可见

下面是我用来解决这个问题的代码(AngularJS):

如果不使用Angular,请使用以下命令:

vectorLayer.getSource().removeFeature(feature);
vectorLayer.setVisible(false);
vectorLayer.setVisible(true);

我也遇到了类似的问题。反复调用setVisible对我来说不起作用

事实证明,如果您从图层中删除选定的特征,它将被删除,但仍然可见。在您选择其他特征之前,它不会被“视觉”删除

我所做的:

// collection that will contain all selected features
var selectFeatures = new ol.Collection();
// setting up the select interaction
var selectInteraction = new ol.interaction.Select({
  features: selectFeatures
});

// custom event to clear all selections
selectInteraction.on('clearSelections', function() {
   selectedFeatures.clear();
});



removeSomeFeaturesFromLayer() {
   // dispatch the event first
   selectInteraction.dispatchEvent({
      type: 'clearSelections'
   });

   /* 
    * NOW LOOP THROUGH THE FEATURES IN THE LAYER
    * AND REMOVE WHATEVER ONES YOU WANT
    */

}

我希望这能有所帮助。

你能提供你所有的代码吗,或者最好做一个小提琴来向我们展示你的案例??这可能与你声明源代码、功能等的方式有关。你提供的代码只是一棵树。source.clear()应该删除您的所有功能automatically@pavlos.我想,我提供了所有必要的部分。在第一段代码中,
功能
是功能的一般集合(仅就OpenLayers 3 API而言),
源代码
是一般向量源代码(如果它的意思不同,我不会称之为“源代码”).在第二段代码中,我使用标准ol3函数
getArray
将功能集合转换为数组。因此,我在这里不提供任何非标准功能,只提供普通功能和普通源代码,正如OpenLayers文档中所述。将功能集合转换为数组会导致同步能力下降。M可能这就是你的情况。尝试使用ol.Collection。在清除指向我你的源未同步后选择功能。同时尝试删除该功能,获取向量层的新源-->
vectorLayer.getSource()。删除功能(yourfeature)
。您也可以尝试从select interaction中移除fetures-->
select\u interaction.getFeatures().clear()
。正如您所见,事情并不是那么简单。除非您提供更好的代码图像,否则我只能猜测。@pavlos。谢谢!我今天会检查它。
// collection that will contain all selected features
var selectFeatures = new ol.Collection();
// setting up the select interaction
var selectInteraction = new ol.interaction.Select({
  features: selectFeatures
});

// custom event to clear all selections
selectInteraction.on('clearSelections', function() {
   selectedFeatures.clear();
});



removeSomeFeaturesFromLayer() {
   // dispatch the event first
   selectInteraction.dispatchEvent({
      type: 'clearSelections'
   });

   /* 
    * NOW LOOP THROUGH THE FEATURES IN THE LAYER
    * AND REMOVE WHATEVER ONES YOU WANT
    */

}