Openlayers: ol.interaction.Select: style option doesn't take effect when feature has style

Created on 30 May 2016  路  4Comments  路  Source: openlayers/openlayers

Description as title. To show this issue, I create a demo on jsfiddle. The blue feature will be changed to red after selected. But the green feature will not. Because the green feature has its own style. Is it intended? Or is it a bug? Thanks !

Most helpful comment

This is intended. A feature style will always override a style defined on a layer or an interaction. There are several ways to achieve what you want. I created an update JSFiddle that shows one of them: https://jsfiddle.net/rdztsnwe/4/. Basically what you do is this:

var selectStyle = new ol.style.Style(/* ... */);
var select = new ol.interaction.Select({
  style: selectStyle;
});
function selectableStyle(style) {
  return function() {
    return select.getFeatures().getArray().indexOf(this) == -1 ? selectStyle : style
  }
};
var feature = new ol.Feature(/* ... */);
feature.setStyle(selectableStyle(new ol.style.Style(/* ... */)));

All 4 comments

Any comment? Can this be applied also to ol.Interaction.Modify?

This is intended. A feature style will always override a style defined on a layer or an interaction. There are several ways to achieve what you want. I created an update JSFiddle that shows one of them: https://jsfiddle.net/rdztsnwe/4/. Basically what you do is this:

var selectStyle = new ol.style.Style(/* ... */);
var select = new ol.interaction.Select({
  style: selectStyle;
});
function selectableStyle(style) {
  return function() {
    return select.getFeatures().getArray().indexOf(this) == -1 ? selectStyle : style
  }
};
var feature = new ol.Feature(/* ... */);
feature.setStyle(selectableStyle(new ol.style.Style(/* ... */)));

Yeah, thanks for the quick answer. I found this resource that helped me. http://stackoverflow.com/questions/32524122/how-to-apply-a-style-to-the-modify-interaction-in-openlayers-3

"A feature style will always override a style defined on a layer or an interaction."

do I understand it correctly, that we cannot use both style altogether but rather move it to a (dynamic) feature style alone?

Was this page helpful?
0 / 5 - 0 ratings