Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an event for when features are selected in OpenLayers 3?

http://ol3js.org/en/master/examples/select-features.html

Given the above examples, what extension points are there for hooking into when features are selected?

like image 628
Poul K. Sørensen Avatar asked May 03 '14 23:05

Poul K. Sørensen


2 Answers

Here is a solution that might be more intuitive than Danny's, and also seems to be the "official" way, see this issue on ol3's GitHub.

Simply add the listener to the collection of selected features :

    mySelectInteraction.getFeatures().on('change:length', function(e) {
        if (e.target.getArray().length === 0) {
            alert("no selected feature");
        } else {
            var feature = e.target.item(0);
            alert(feature.getId()); //or do something better with the feature !
        }
    });
like image 156
Pierre Henry Avatar answered Nov 14 '22 21:11

Pierre Henry


You can bind a precompose event to your layer when a singleclick event is triggered on your map. From here you can dispatch a change event on your select interaction.

yourmap.on('singleclick',function(event)){
    layer.once('precompose',function(event){
        yourSelectInteraction.dispatchChangeEvent();
    }
}

yourSelectInteraction.on('change',function(){
    //Do stuff with your selected features here
}
like image 25
Danny Hoek Avatar answered Nov 14 '22 22:11

Danny Hoek