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?
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 !
}
});
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
}
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