Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all features from the Openlayers 3 viewport

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;
            });
});
like image 438
User1836 Avatar asked Aug 05 '16 09:08

User1836


1 Answers

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 
}); 
like image 118
Hicham Zouarhi Avatar answered Sep 21 '22 09:09

Hicham Zouarhi