OpenLayers 3 – How to Get Only Modified Feature

javascriptopenlayers

I have a problem with OL3 and it's ol.interaction.Modify Event. Whenever I modify a feature, just one, it returns all features inside the layer. Currently I have 77 features (polygons) and I can modify them with the modify interaction. Inside the callback function it returns the event with the feature member array and this seems to be buggy:

modify.on('modifyend', function(e) {

    console.log('=== Modify end ===');
    var modifiedFeature = e.features.item(0);

    //Why do I get 77 modified features if I have only touched one?
    // e.features size is = 77
    console.log(e.features);
});

Do you know a working way to get the one modified feature?

Best Answer

I guess you pass all the features to the modify interaction. That is why it returns all of them (If you dont provide your code I can only guess). You can always use a select interaction and then pass the selected feature to modify interction. For your case you may iterate through your features and check for the revision, each time a feature is modified, its version number will be incremented.

So do something like this

var modifiedFeatures;
modify.on('modifyend', function(e) {
    var features = e.features.getArray();
    console.log(features.length);
    for (var i = 0; i < features.length; i++) {
        var rev = features[i].getRevision();
        if (rev > 1) {
            console.log("feature with revision:" + rev + " has been modified");
            modifiedFeatures.push(features[i]);
        }
    }
});

check this fiddle.