1

I have a feature that has a custom style (color). When I click on it, it does not highlight. However, if I edit the code by commenting out the myFeature.setStyle(myStyle) call it highlights as expected.

Is there a way to force it to highlight when a style already exists for the feature?

Code sample:

const myFill = new Fill();
myFill.setColor('rgba(100,100,200,0.60)');
const myStyle = new Style({myFill});
const myFeature = new Feature(someObject);
myFeature.setStyle(myStyle);
mySource.addFeature(myFeature);

//...
const select = new Select({condition:click});
map.addInteraction(select);

I tried adding a style to the Select but no luck.

const select = new Select({condition:click, style: myHighlightStyle});
ekjcfn3902039
  • 1,673
  • 3
  • 29
  • 54
  • 4
    A select style won't override a feature style, but it will override a layer style so it's best to style your features via a style function on the layer. – Mike Feb 28 '19 at 20:35
  • I see that works, thanks! However, I have many different Features within a layer each with their own color. Is there a way to account for this? – ekjcfn3902039 Feb 28 '19 at 20:56
  • As long as there's a way to associate the feature with the color it can be done via a style function, similar to this question perhaps https://stackoverflow.com/questions/54930198/how-to-add-many-markers-with-custom-text-to-openlayers-5 – Mike Feb 28 '19 at 21:36
  • See also this example of an extensive style function: https://openlayers.org/en/v5.3.0/examples/resources/mapbox-streets-v6-style.js – bennos Mar 01 '19 at 08:46

0 Answers0