Search code examples
openlayersopenlayers-3

Draw interaction. How to prevent removing of vertices on clicking on them


There is a feature in OpenLayers 3 so that, when you draw some object and just click on a vertex of some geometry, then this vertex gets disappeared. So, for example, a line with three vertices may become a line with two vertices. One of my users called this feature a nuclear weapon, because it may turn the whole interaction with a map into a mess - say, for example, you are drawing some features one near the other and click on one vertex unintentionally. In this case the whole feature gets destroyed. So, how can we prevent this behaviour?


Solution

  • Removing a vertex by of a vector feature by clicking on it is a normal behavior that comes from the ol.interaction.Modify. Its default condition is: when a single click occurs on it. See it here: in the modify.js source code.

    See also the documentation of the modify interaction. See the deleteCondition. That would be a first possible solution for you. You could set a condition so that it would take more than just a plain single click for this feature to be triggered. That would fix your issue.

    You say that this occurs while drawing. It may also be a good idea not to have a modify interaction active while drawing. That's a second good solution for you. I would also personnaly highly suggest to go with this approach. In a map, there should always be only one tool active at a time. Drawing and modifying features should never be active at the same time.