[英]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)
有没有找到来源图层或删除要素而不指定从何处的好方法?
目前,我正在捕获异常,但这在很多层次和资源上变得笨拙。
对于每个来源,您可以尝试获取选定的功能。 如果响应不为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;
}
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.