4

I am working on the delete option by double click in OpenLayers. I use the following code:

 map.on('dblclick', function(evt) {
  var selectCollection = selectInteraction.getFeatures();
  if(selectCollection.getLength() > 0){
   var answer = window.confirm("Do you want to delete?")
   if (answer) {
   vectorLayer.getSource().removeFeature(selectCollection.item(0));
    }
  }
 }); 

based on these examples:

https://gis.stackexchange.com/questions/126581/openlayers-3-capturing-various-click-events

How to create a dialog with “yes” and “no” options

everything would be perfect, but I cannot delete the linestrings and points at some point.

As you can see below, all the shapes which have the "filled surface" can be deleted easily after the selection.

enter image description here

I am getting the

**Vector.js:906

Uncaught TypeError: Cannot read properties of undefined (reading 'forEach')**

instead.

I don't know how to fix it frankly. By reading this hint:

Cannot read property 'forEach' of undefined

I think, that I should use the push method, although I see no option for distinguishing my selectInteraction between the separate geometry.

My full JSFiddle is here:

https://jsfiddle.net/skwbdx0j/

Is there any option to fix it?

UPDATE:

Placing a certain variable inside the getSource didn't resolve this issue.

 map.on('dblclick', function(evt) {
 var selectCollection = selectInteraction.getFeatures();
 if(selectCollection.getLength() > 0){
 var answer = window.confirm("Do you want to delete?")
  if (answer) {
  
  vectorLayer.getSource(lineInteraction).  // the same line
   removeFeature(selectCollection.item(0));           }
      }       
   });
Geographos
  • 827
  • 2
  • 23
  • 57
  • You would get that error if you tried to remove the same feature twice. Removing a feature from the source will not automatically remove it from the select collection. – Mike Oct 21 '21 at 16:13
  • What do you mean? – Geographos Oct 21 '21 at 16:26
  • Ok, I got you. No! The error comes even from the first code example. I don't know how to fix it. – Geographos Oct 21 '21 at 16:41
  • After removing `item(0)` from the source remove it from the collection `vectorLayer.getSource().removeFeature(selectCollection.item(0)); selectCollection.removeAt(0);` – Mike Oct 21 '21 at 16:54
  • The following one 'vectorLayer.getSource(lineInteraction).removeFeature(selectCollection.removeAt(0));' still doesn't work. The error is exactly the same. – Geographos Oct 22 '21 at 07:43

0 Answers0