0
votes

I'm trying to pull a feature off my openlayers map object and change the color of the feature on the map.

olMap.getLayers().getArray() 

is what I have been using to get the layers. Are these actually features? I want to apply a simple style such as:

    let selected_polygon_style = {
      strokeWidth: 5,
      strokeColor: '#ff0000'
    };

when I call something like olMap.getLayers().getArray()[0].getSource().getFeatures(); I get undefined function.

1

1 Answers

0
votes

getFeatures will be undefined if layer[0] isn't a vector layer and calling it will cause an error. To find features in any of the map's layers check if getFeatures is defined for the layer source before calling it:

olMap.getLayers().getArray().forEach(function(layer){
  if (layer.getSource().getFeatures) {
    var features = layer.getSource().getFeatures();
    ....
  }
});