Open Layers 3: How to create listener for a modify interaction

Alex picture Alex · Jul 13, 2014 · Viewed 9.8k times · Source

I have managed to set up a modify interaction.

The docs for ol.interaction.Modify (http://ol3js.org/en/master/apidoc/ol.interaction.Modify.html) don't metion a single event that is fired when a feature is modified.

Unlike for instance for ol.interaction.Draw (http://ol3js.org/en/master/apidoc/ol.interaction.Draw.html) which works nicely.

I need to update the coordinates in the database when a feature has been modified.

How can I set up a listener?

Answer

Alex picture Alex · Jul 19, 2014

I found a solution.

The high-level-explanation is here: http://boundlessgeo.com/2014/06/openlayers-editing-wfs-t/

Basically you don't listen to changes in the modify-interaction (like you do in the draw-interaction). Instead, you listen to changes in the selected features themselves.

Here's a short extract:

// get the features from the select interaction
var selected_features = select_interaction.getFeatures();
// when a feature is selected...
selected_features.on('add', function(event) {
    // get the feature
    var feature = event.element;
    // ...listen for changes on it
    feature.on('change', function(event) {
        // got it!
    });
});

Here is a complete working example: http://codepen.io/barbalex/pen/fBpyb