OpenLayers 3 get extent of all feature contents in a list

FatAl picture FatAl · May 3, 2015 · Viewed 7.7k times · Source

I want to zoom on the extent of all the features contained in a list.

Firstly, I put my feature in a list:

selectedFeatures = [];    
vector2.getSource().forEachFeature(function(feature) {
                var att = feature.get("NOM");
                if (att == strUser) {
                    selectedFeatures.push(feature);
                    }
                });

Secondly, here my problem... i want to zoom on the extent of all the feature on my list "selectedFeatures"

I try this but always return me an infinite extent:

var vectorSource = new ol.source.Vector({
        projection: 'EPSG:3857',
        features: selectedFeatures //add an array of features
        });

var dataExtent = vectorSource.getExtent();
map.getView().fitExtent(dataExtent, map.getSize())
console.log("Extents : " + dataExtent);

Someone have a solution to get the extent of features contained in a list ?

Answer

Poul K. Sørensen picture Poul K. Sørensen · May 4, 2015

This should do the trick?

var extent = features[0].getGeometry().getExtent().slice(0);
features.forEach(function(feature){ ol.extent.extend(extent,feature.getGeometry().getExtent())});