3

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...

  • use shift +mouse click delete vertices in the official example. – Mapperz Jun 1 '15 at 15:59
  • I notice that the behaviour has changed between v3.4.x and v3.5.x. Previously it would just delete the vertex and ignore the draw interaction (openlayers.org/en/v3.4.0/examples/draw-and-modify-features.html). If you disable the draw interaction then it works, but adding a condition to the draw interaction to try and counter the behaviour didn't work for me jsfiddle.net/8d3ygyqt – Guy Jun 1 '15 at 17:30
  • Ah yes you are right, the version 3.4.x seems to work how I would envisage it. Perhaps it is a bug in 3.5.x then? – marlowp Jun 2 '15 at 7:30
  • I'd say so. There is nothing obvious in the release notes to indicate this change is intentional (github.com/openlayers/ol3/releases/tag/v3.5.0), and I've at least thought adding a condition in the draw interaction (like in my demo above) should work. github.com/openlayers/ol3/issues/new (I can raise it if you like?) – Guy Jun 2 '15 at 8:25
  • Yes ok if you could raise it that would be great, thank you. – marlowp Jun 2 '15 at 8:31
1

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.