Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

openlayers-3

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?

like image 558
Alex Avatar asked Jul 13 '14 20:07

Alex


2 Answers

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

like image 91
Alex Avatar answered Jan 03 '23 13:01

Alex


for this, you have to use like following :

feature.once('change', function(bool) {if (bool) {document.getElementById('my_input_text').value='feature changed'}})

you have to use 'once' instead of 'on' to avoid multiple check and use boolean variable to check if feature is changed or not.

Hope this help

like image 26
jamal Avatar answered Jan 03 '23 15:01

jamal