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.
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)
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With