OpenLayers Polygon – Enabling Users to Cancel Completion and Continue Drawing in OpenLayers

draw-interactionopenlayers

I am using the OpenLayers v4.6.5 Draw interaction.

drawInteraction = new ol.interaction.Draw({...});
map.addInteraction(drawInteraction);

I want users to be able to add properties to the features they draw so use the drawend event to display a form.

drawInteraction.on('drawend', function(e) {...}).

This all works fine.

However, an issue that arises is that, when drawing polygons, users sometimes complete polygons without intending to (for example by clicking within the snap distance of the start point or clicking too fast so triggering a double click event). On simple polygons they can easily cancel and start again, but many of our polygons are complex.

I wondered if there is a way, once the form is displayed, of undoing their last click and cancelling the completion of the geometry? I imagine them clicking an "Undo last click" button on the form and just carrying on with drawing their polygon. I have explored removeLastPoint (but this seems to have no effect once the drawing has finished) and finishCondition (but cannot see how to use it achieve what I want).

Users are able to modify their incorrect polygons to get them to be what they want, so it is not a huge issue. I understand I could make it harder for them to accidentally complete their polygons, by changing snap distances etc.

But an undo functionality would make it more intuitive for them. How can it be done?

Best Answer

finishCondition: function(event) { return confirm("Are you sure?"); } works for me.