1

In OpenLayers 6...

I'm not familiar with the thing, but... How can I iterate through all features on my layer with the title "firmenGeoJSON" and hide or show some of them with specific property "name"?

Thank you...

let myLayers = map.getAllLayers();

myLayers.forEach(function(layer) {
    if (layer.getProperties()['title'] == 'firmenGeoJSON') {
        console.log('found and hide');
        //??????????
    };
});
snowman
  • 11
  • 1

1 Answers1

2

To iterate through all features of a layer you need to get its source first:

layer.getSource().forEachFeature(function (feature) {
    console.log(feature);
});

Though if you want to show or hide features it may be better to do this in a layer style function:

import {Circle, Fill, Stroke, Style} from './style.js';

const style = new Style({
    image: new Circle({
        radius: 8,
        fill: new Fill({
            color: 'blue',
        }),
    }),
    stroke: new Stroke({
        width: 4,
        color: 'green',
    }),
});
layer.setStyle(function (feature, resolution) {
    if (feature.get('hidden')) {
        return undefined;
    }
    return style;
});

But note that a layer style function is only called if the Feature itself has no Style set. In that case consider switching to a layer style function, as seen above.

MoonE
  • 503
  • 3
  • 5