[GIS] OpenLayers 3 – Modify Feature – Delete vertex on keypress

eventsopenlayers

I'm trying to create functionality in OpenLayers 3 that allows me to delete a vertex from a feature that is currently selected using the modify interaction.

I've managed to see that there is a 'deleteCondition' parameter set on the modify interaction when it is created, and that this needs to return an ol.events.condition object. At present I can see from this example (http://openlayers.org/en/master/examples/draw-and-modify-features.html) that is it possible to delete a vertex by pressing SHIFT and mouse click at the same time, however this then creates a new point which seems counter-intuitive to me. If you are deleting a vertex why would you want to create a new vertex in exactly the same place? I may have missed something here though.

So the question is how can I use these ol.events.condition objects to make it so that if the user clicks a specific key on the keyboard the highlighted vertex is deleted?

My initial thoughts were to have something like this:

var modifyInteraction = new ol.interaction.Modify({
    features: selectInteraction.getFeatures(),
    deleteCondition: function(event) {  

        var key = event.originalEvent.keycode || event.originalEvent.charCode;
        if(key == 46) {
            return  ol.events.condition.always(event);
        } else {
            return ol.events.condition.never(event);
        }
    }
});

But this produces some very strange results…

Best Answer

I think there is a problem with freehandCondition in Draw interaction, which is ShiftKeyOnly by default. When I changed it to ol.events.condition.never I can delete vertex without creating new feature. This solution deactivates freehand drawing. Example:

draw = new ol.interaction.Draw({
features:features,
type: ('Polygon'),
freehandCondition: function(event){
return ol.events.condition.never(event);
}