Search code examples
javascriptopenlayers

How to zoomToExtent with useSpatialIndex = false?


I have some problems with extents. I use Openlayers and a Mapserver WFS layer, with geojson as output format. I need to get my features ordered in my geojson, so I set useSpatialIndex=false in the source vector.

But the problem is that I want to fit zoom with the size of the map, for different screen sizes. So, is there a way to adjust zoom to layer extent without the function getExtent ?


Solution

  • You can calculate it by iterating through the features

      var extent = ol.extent.createEmpty();
      source.forEachFeature(function(feature){
        ol.extent.extend(extent, feature.getGeometry().getExtent(), extent);
      });
      console.log(extent);