我有一个包含多个图层的 map ,所有图层都连接到不同的矢量源。
当用户选择某个功能时,我希望他能够删除该功能。但是,我似乎无法找到一种方法来定位该功能所在的源层。
如果我尝试从所有图层中删除该功能,则会收到错误:
Vector.js:946 Uncaught TypeError: Cannot read property 'forEach' of undefined
at Vector.removeFeatureInternal (Vector.js:946)
有没有一种好的方法可以在不指定从哪里的情况下查找源层或删除功能?
目前我正在捕获异常,但是对于很多层和源来说这变得很笨拙。
最佳答案
对于每个来源,您可以尝试获取所选的功能。如果响应不为 null
,则该功能存在于该源上。
在你的选择中沿着这条路的东西:
const featureId = selectedFeature.getId()
map.getLayers().getArray().forEach(layer => {
const source = layer.getSource();
if (source instanceof VectorLayer) {
const featureExists = source.getFeatureById(featureId);
if (featureExists) {
source.removeFeature(selectedFeature);
return;
}
}
})
关于javascript - 如何删除具有多个可能来源的 openlayers 中的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55457329/