Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch event after drawend added to source in OL3?

Tags:

openlayers-3

I'd like to update a list after each feature has been drawn to a map.

When I use drawend to catch the finishing of a drawing, the feature being drawn is not yet added to the vector source at that moment.

So

var draw = new ol.interaction.Draw({
    source: source,
    type: 'Point'
});

draw.on('drawend', function () {
    console.log(source.getFeatures().length)
});

map.addInteraction(draw);

Will output 0 when the first point has been added.

How can I catch the state of the map when the drawing is finished and the feature has been added to the vector source? Thus I'm looking for a state when source.getFeatures().length would be 1 on an empty map.

like image 223
hyperknot Avatar asked Aug 29 '16 22:08

hyperknot


1 Answers

You can always try what @jonatas suggest. It should do the job you look for. One more workaround is to get the currently drawn feature from the event itself and add it to your features array. Check this

draw.on('drawend', function (e) {
var currentFeature = e.feature;//this is the feature fired the event
var restOfFeats = source.getFeatures();//rest of feats
var allFeats = restOfFeats.concat(currentFeature);//concatenate the event feat to the array of source feats
console.log(allFeats.length)
});
like image 118
pavlos Avatar answered Nov 29 '22 06:11

pavlos