wmaddox
wmaddox

Reputation: 160

automatically select feature after drawend

I have a Select and Draw interaction in openlayers 3 (v3.9.0) and I'd like to add some unique behavior to it. Currently, after a feature is drawn, I have to click on the feature to select it. Is there a way to bypass the click event altogether and have the feature auto select itself on drawend?

Thanks

Upvotes: 3

Views: 1132

Answers (3)

David
David

Reputation: 336

You can simply call getFeatures() on the ol.interaction.Select then add the new feature to this observable collection:

selectCtrl = new ol.interaction.Select();
drawCtrl = new ol.interaction.Draw();

drawCtrl.on("drawend",function(e){
      selectCtrl.getFeatures();
      features.push(e.feature);
});

Upvotes: 2

user3748764
user3748764

Reputation: 346

If, like me, you also want to automatically leave drawing mode and return to selection mode after the drawing ends (and the feature is selected), you can do something like this:

mySelect = new ol.interaction.Select();
myDraw = new ol.interaction.Draw();
lastDrawnFeature = null;

myDraw.on('drawend',function(e){
    lastDrawnFeature = e.feature;

    // switch to select interaction
    myDraw.setActive(false);
    mySelect.setActive(true);
});

mySelect.on('select',function(e){
    if (lastDrawnFeature) {
        // Actual selection has to be done here,
        // otherwise the last point drawn will be selected instead.
        mySelect.getFeatures();
        features.clear();
        features.push(lastDrawnFeature);
        lastDrawnFeature = null;
    }
});

Upvotes: 1

wmaddox
wmaddox

Reputation: 160

Solved it. ol.interaction.select fires AFTER the draw.on('drawend',()) resolves itself. The trick is to force select.condition to return false after a new feature has been added. See the use of selectedFeature.push(evt.feature) and var featureadded in my jsfiddle for details.

http://jsfiddle.net/williemaddox/0um2ud3v/

Upvotes: 1

Related Questions