Robert Smith
Robert Smith

Reputation: 779

beforefeatureadded event equivalent for OpenLayers 3

For openlayers 2, you had an event called beforefeatureadded that you could do validation before actually adding a new feature. What is the equivalent of beforefeatureadded event for OpenLayers 3?

OpenLayers 2 example:

layer.events.register("beforefeatureadded", layer, validationFunction);

Upvotes: 1

Views: 283

Answers (1)

ahocevar
ahocevar

Reputation: 5647

The equivalent to the OpenLayers 2 beforefeatureadded event is the use of a staging collection for drawn features:

var source = new ol.source.Vector();
var features = new ol.Collection();
features.on('add', function(evt) {
  var feature = evt.element;
  if (conditionMet(feature)) {
    source.addFeature(evt.element);
  }
  // clear the staging collection
  features.pop();
});

What's also possible is the use of a Draw condition, and this is what @robert-smith actually wants here:

var draw = new ol.interaction.Draw({
  condition: function(evt) {
    return ol.events.condition.noModifierKeys(evt) && conditionMet(evt);
  }
});

Upvotes: 2

Related Questions