[英]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.