简体   繁体   English

无法删除OpenLayers 3中的功能

[英]Unable to remove feature in OpenLayers 3

I have read dozens of threads at stackoverflow, but none of them helps. 我在stackoverflow上读了几十个线程,但是它们都没有帮助。 So, this is what I try to do: 因此,这就是我尝试做的事情:

features.forEach(function(feature){
    source.removeFeature(feature); 
    console.log("removed");
    console.log(feature);
});

As a result, when I have only one feature selected, I see in the console these messages: 结果,当我仅选择一项功能时,我在控制台中看到以下消息:

removed Controller.js:525:8
Object { disposed_: false, onDisposeCallbacks_: undefined, ...}

In terms of what I see in the console, everything looks good. 就我在控制台中看到的内容而言,一切看起来都不错。 But the problem is that the feature is not removed from the map. 但是问题在于该功能并未从地图中删除。

EDIT 编辑

Now it is even more interesting. 现在,它变得更加有趣。 If I convert features to array with getArray and do this: 如果我使用getArray将功能转换为array并执行以下操作:

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

when I have a lot of features and only one feature selected, then in this case only this selected feature remains and all the rest features are removed. 当我有很多功能并且仅选择了一个功能时,在这种情况下,仅保留该选择的功能,并删除所有其余功能。 What the heck is going on?? 到底他妈发生了什么??

I had this issue for a long time and I could not figure it out. 我这个问题已经很长时间了,我无法弄清楚。 As it turns out, it seems to be a refresh issue in OpenLayers. 事实证明,这似乎是OpenLayers中的一个刷新问题。 The way I found to get the layer to refresh is to make it invisible and then visible again. 我发现刷新图层的方法是使其不可见,然后再次可见。

Here is the code I used to solve the problem (in AngularJS): 这是我用来解决问题的代码(在AngularJS中):

vectorLayer.getSource().removeFeature(feature);
$timeout(function() {
    vectorLayer.setVisible(false);
    vectorLayer.setVisible(true);
}, 10);

If you're not using Angular, just use the following: 如果您不使用Angular,请使用以下命令:

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

I had a similar issue. 我有一个类似的问题。 The repeated calls to setVisible did not work for me. 反复调用setVisible对我不起作用。

Turns out if you remove a selected feature from a layer, it will be removed, but it will still be visible. 原来,如果您从图层中删除选定的要素,则该要素将被删除,但仍然可见。 And it won't be removed "Visually" until you select something else. 除非您选择其他选项,否则不会“视觉上”将其删除。

What I did: 我做了什么:

// 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
    */

}

I hope that helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM