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?
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?
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
}
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 !
}
});
e.target.getLength()
instead of e.target.getArray().length
–
Thomasinathomasine 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
}
You want to use the featureselected
event handler:
This example is on a Vector layer:
featuresLayer.events.on({
'featureselected': function (e) {
console.log(e);
}
});
© 2022 - 2024 — McMap. All rights reserved.