繁体   English   中英

无法删除OpenLayers 3中的功能

[英]Unable to remove feature in OpenLayers 3

我在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();

当我有很多功能并且仅选择了一个功能时,在这种情况下,仅保留该选择的功能,并删除所有其余功能。 到底他妈发生了什么??

我这个问题已经很长时间了,我无法弄清楚。 事实证明,这似乎是OpenLayers中的一个刷新问题。 我发现刷新图层的方法是使其不可见,然后再次可见。

这是我用来解决问题的代码(在AngularJS中):

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

如果您不使用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
    */

}

希望对您有所帮助。

暂无
暂无

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

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