如何删除具有多个可能来源的 openlayers 中的功能
How to remove a feature in openlayers with multiple possible sources
我有一个多层地图,所有图层都连接到不同的矢量源。
当用户选择一个功能时,我希望他能够删除该功能。但是,我似乎无法找到一种方法来定位要素所在的源层。
如果我尝试从所有图层中删除该功能,我会收到错误消息:
Vector.js:946 Uncaught TypeError: Cannot read property 'forEach' of undefined
at Vector.removeFeatureInternal (Vector.js:946)
有没有什么好的方法可以在不指定来源的情况下找到源层或删除特征?
目前我正在捕获异常,但是对于很多层和源来说这变得笨拙。
对于每个来源,您可以尝试获取 selected 功能。如果响应不是 null
,则该源上存在该特征。
你的 select:
里面有这样的东西
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;
}
}
})
我有一个多层地图,所有图层都连接到不同的矢量源。
当用户选择一个功能时,我希望他能够删除该功能。但是,我似乎无法找到一种方法来定位要素所在的源层。
如果我尝试从所有图层中删除该功能,我会收到错误消息:
Vector.js:946 Uncaught TypeError: Cannot read property 'forEach' of undefined
at Vector.removeFeatureInternal (Vector.js:946)
有没有什么好的方法可以在不指定来源的情况下找到源层或删除特征?
目前我正在捕获异常,但是对于很多层和源来说这变得笨拙。
对于每个来源,您可以尝试获取 selected 功能。如果响应不是 null
,则该源上存在该特征。
你的 select:
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;
}
}
})