简体   繁体   English

OpenLayers 3中的源代码清除BUG

[英]source clear BUG in OpenLayers 3

I'm using OpenLayers 3 (v3.20). 我正在使用OpenLayers 3(v3.20)。 What I want to achieve is just to remove all features from a particular layer. 我要实现的只是从特定图层中删除所有功能。 I see that there is a clear method and documentation says, that 我看到有一个明确的方法,文档说,

clear(opt_fast) 清除(opt_fast)

Remove all features from the source. 从源中删除所有功能。

However, when I apply it to my layer source like so: 但是,当我像这样将其应用于图层源时:

layer.getSource().clear();

I see a blink (features are removed) and then I see a server request, so that features are reloaded again. 我看到闪烁(功能已删除),然后看到服务器请求,以便再次重新加载功能。 So, either documentation is incomplete, or there is a bug. 因此,要么文档不完整,要么存在错误。

I also tried to remove features like so: 我还尝试删除以下功能:

features = source.getFeatures();
for (i = 0; i < features.length; i += 1) {
    source.removeFeature(features[i]);
}

But it works really strange. 但这确实很奇怪。 If, for example, I have four features, when I loop once, it removes just two features and when I loop twice, one extra feature is removed. 例如,如果我有四个功能,则当我循环一次时,它将仅删除两个功能,而当我循环两次时,将删除一个额外的功能。 All in all, I have to loop three times (which is indeed not DRY) to remove all features. 总而言之,我必须循环3次(实际上不是DRY)才能删除所有功能。 I really wonder, why is that and how can I fix it. 我真的很好奇,为什么会这样以及如何解决它。 Thanks! 谢谢!

As pointed by Karl-Johan Sjögren, removing a array member when iterate through it modifies the array itself, so, you use reverse array or use a native function from Array MDN reference : 正如Karl-JohanSjögren指出的那样,在迭代遍历时删除数组成员会修改数组本身,因此,您可以使用反向数组或使用Array MDN参考中的本机函数:

features = source.getFeatures();
features.forEach(function (feature){
  source.removeFeature(feature);
});

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

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