Openlayers 4.11 : no style applied when programati

2019-09-15 03:50发布

问题:

I'm using the accepted answer from here to programatically select a feature on my map.

All ok, except that my custom select style is not applied (the element looks just like before).

I'm using this code to setup my Select interaction:

var selectSingleOrDoubleClick = new ol.interaction.Select({
    style: this.selectCustomStyle,
    condition: function(mapBrowserEvent) {
        return ol.events.condition.singleClick(mapBrowserEvent) || ol.events.condition.doubleClick(mapBrowserEvent);
    },
    filter: function(feature, layer) {
        ...
    }
});

I'm doing this in order to programmatically select my feature:

selectSingleOrDoubleClick.getFeatures().clear();
selectSingleOrDoubleClick.getFeatures().push(featureToSelect);

selectControl.dispatchEvent({
    type: 'select',
    selected: [featureToSelect],
    deselected: []
});

I should not that my feature IS considered selected (for example if I select another feature via user interaction, my previously-selected feature becomes unselected, etc.

回答1:

Apparently, this is normal behavior in Openlayers, whenever the Features involved in your selection already have a non-default style applied.

So if you want to fix this, then you need to use select/unselect events to apply whatever style you need to have applied, and remove it as necessary.



标签: openlayers