9

I am trying to find out all the features which are visible (viewport) on a layer in Openlayers 3.

I am able to find out a single feature if I add a click event to the map which is as follows. But I am not able to find all the features which are visible in the viewport. Could anyone help with this?

map.on('click', function(evt) {
        var feature = map.forEachFeatureAtPixel(evt.pixel,
            function(feature, layer) {
                return feature;
            });
});
ahocevar
  • 5,448
  • 15
  • 29
User1836
  • 151
  • 3
  • 13

1 Answers1

16

I propose that first you get the extent of the view :

var extent = yourMap.getView().calculateExtent(yourMmap.getSize());

then get all features within this extent :

yourVectorSource.forEachFeatureInExtent(extent, function(feature){
    // do something 
}); 
Hicham Zouarhi
  • 1,030
  • 1
  • 18
  • 28
  • What if the feature is styled off? I mean, if the feature is filtered and not showing on the map? The yourVectorSource.forEachFeatureInExtent() will still return the feature by I wanted to know which are not rendered on the map... any way to know that? – Falcoa Jul 31 '23 at 00:17