我试图从OpenLayers 4地图中删除一系列功能。我不想清除源代码中的所有功能。在数组中添加了几个选定的功能。
目前,我正在迭代数组并使用此数组。source.removeFeature(array[index]);
与多次循环不同,是否有任何方法一次删除数组中的特性?
注意:我不想删除源代码中的所有特性,而只是删除添加到数组中的特性。
代码:
var docketSource = new ol.source.Vector({
url: dataUrl,
format: new ol.format.TopoJSON()
});
var redFeatures = [];
function removeRedFeature(redFeatures) {
for (var i = 0; i < redFeatures.length; i++) {
docketSource.removeFeature( redFeatures[i] );
}
}
发布于 2018-05-28 06:03:33
假设您的source
是一个ol.source.Vector
,您可以通过以下操作执行clear()
:
source.clear()
我有点搞不懂你在问什么,但你提到了“精选功能”。也许这就是你想要的?
var select = new ol.interaction.Select();
select.getFeatures().forEach(function(feature){
docketSource.removeFeature(feature);
});
有关详细信息,请参阅http://openlayers.org/en/v3.0.0/apidoc/ol.source.Vector.html#clear。
UPDATE:OL 2.13.1中有一个removeFeatures
函数。
removeFeatures:函数(特性、选项) 从图层中移除特征。这将删除所有绘制的功能,并将其从层的控件中删除。将为每个功能触发前特征化事件和特征化事件。功能删除事件将在所有功能被删除后触发。若要抑制事件触发,请使用静默选项。
请参阅:http://dev.openlayers.org/releases/OpenLayers-2.13.1/doc/apidocs/files/OpenLayers/Layer/Vector-js.html#OpenLayers.Layer.Vector.removeFeatures
发布于 2018-05-29 02:11:21
您的解决方案是一种解决方案,但也可以使用docketSource.getFeaturesCollection()
返回ol.Collection
of ol.Feature
s。然后,可以操作返回的集合并使用函数remove
。
因此,您可以执行docketSource.getFeaturesCollection().remove(yourfeature);
,但是,如果您需要循环遍历集合,您将使用集合的forEach
方法,它将类似于您的解决方案。
看看 methods,看看它们是否能更好地符合你的目的。
发布于 2018-05-28 15:57:13
removeFeature是唯一从ol.source.Vector中删除某些特性的API函数。(见https://github.com/openlayers/openlayers/blob/v4.6.5/src/ol/source/vector.js )
如果引发的事件很多或诸如此类的事件都有问题,可以扩展ol.source.Vector类并实现removeFeatures函数,例如修改clear函数或将clear与removeFeature合并。
https://stackoverflow.com/questions/50559988
复制相似问题