Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenLayers 3 get extent of all feature contents in a list

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 ?

like image 427
FatAl Avatar asked May 03 '15 20:05

FatAl


1 Answers

This should do the trick?

var extent = features[0].getGeometry().getExtent().slice(0);
features.forEach(function(feature){ ol.extent.extend(extent,feature.getGeometry().getExtent())});
like image 174
Poul K. Sørensen Avatar answered Sep 23 '22 01:09

Poul K. Sørensen