我试图从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] );
}
}
答案 0 :(得分:0)
假设您的private final static String CHECKBOXBUTTON_URL = "https://www.w3schools.com/html/tryit.asp?filename=tryhtml_Checkbox";
private final static By CHECKBOX = By.xpath("//input[@name='vehicle' and @value='Car']");
@Test
public void checkbox()
{
System.setProperty("webdriver.edge.driver", "MicrosoftWebDriver.exe");
WebDriver driver = new EdgeDriver();
FluentWait<WebDriver> wait = new WebDriverWait(driver, 0);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.switchTo().frame(1);
// new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframeResult"));
WebElement element=driver.findElement(By.xpath("//input[@name='vehicle' and @value='Car']"));
element.click();
wait.until(ExpectedConditions.elementToBeSelected(element));
element.click();
}
driver.quit(); // break here; checkbox is still checked (but shouldn't)...
}
是source
,您可以执行以下操作ol.source.Vector
:
clear()
我对你提出的问题感到困惑,但你提到了“选定的功能”。也许这就是你所追求的?
source.clear()
有关详细信息,请参阅http://openlayers.org/en/v3.0.0/apidoc/ol.source.Vector.html#clear。
更新: OL 2.13.1 中有var select = new ol.interaction.Select();
select.getFeatures().forEach(function(feature){
docketSource.removeFeature(feature);
});
个功能。
removeFeatures:function(features,options)
从图层中删除要素。这将删除任何绘制的特征,并将其从图层的控件中删除。将为每个功能触发beforefeatureremoved和featureremoved事件。删除所有功能后将触发featuresremoved事件。要禁止事件触发,请使用静默选项。
答案 1 :(得分:0)
您的解决方案是一种解决方案,但您也可以使用返回docketSource.getFeaturesCollection()
ol.Collection
的{{1}}。然后,您可以操作返回的集合并使用函数ol.Feature
所以,你可以做remove
但是,如果您需要遍历集合,则将使用集合的docketSource.getFeaturesCollection().remove(yourfeature);
方法,它与您的解决方案类似。
查看ol.Collection
methods,了解它们是否能更好地符合您的目的。
答案 2 :(得分:-3)
removeFeature是唯一一个从ol.source.Vector中删除某些功能的API函数。 (见https://github.com/openlayers/openlayers/blob/v4.6.5/src/ol/source/vector.js)
如果您遇到大量事件或类似事件的问题,您可以扩展ol.source.Vector类并实现removeFeatures函数,例如修改clear函数或使用removeFeature合并clear。