I have an OpenLayers 4 map with a single click interaction and a corresponding event handler for the "select" event that changes the selected features style.
let clickInteraction = new olInteractionSelect({
layers: function (layer: any) {
return layer.get('selectable') == true;
},
condition: olEventCondition.singleClick,
filter: (feature: any, layer: any) => {
let shouldReturn: boolean = false;
switch (layer.get(this.LAYER_PROPERTY_ID)) {
case this.LAYER_ID_POINTS:
this.onPointClick(feature.getId());
shouldReturn = true;
break;
case this.LAYER_ID_AREAS:
this.onAreaClick(feature.getId());
shouldReturn = true;
break;
default:
break;
}
return shouldReturn;
}
});
let __this = this;
clickInteraction.on('select', function (evt: any) {
let selected = evt.selected;
let deselected = evt.deselected;
//Select and set style for newly selected features
if (selected.length) {
selected.forEach(function (feature: any) {
if (feature.get(__this.FEATUREKEY_SELECTEDSTYLEID)) {
feature.setStyle(__this.createStyleFnFeatureSelected(feature.get(__this.FEATUREKEY_SELECTEDSTYLEID)));
}
});
} else {
deselected.forEach(function (feature: any) {
if (feature.get(__this.FEATUREKEY_STYLEID)) {
feature.setStyle(__this.createStyleFn(feature.get(__this.FEATUREKEY_STYLEID)));
}
});
}
});
this.map.addInteraction(clickInteraction);
Each time a new feature is selected I want to set the style of all previously selected features. What I can't figure out is when the click interaction "select" event is invoked, how do I get a collection of all the previously selected features on the map layer?
There are several thousand features on the map so iterating over all the features is not feasible from a performance perspective. What I want to achieve is to get just those features that were previously selected, not the features selected or deselected in the current click interaction event.
if (selected.length)andelseand process the two forEach loops unconditionally because a single event can contain both the newly select features and the any deselected by that action. If you only require separate styles for selected and not selected you can simply specify a selected style in the interaction constructor and the on select won't be needed.. - Mike