[GIS] openlayers 3 – fit to extent

extentsopenlayerszoom

I am trying something very simple, but strangely it doesn't work.
I need to zoom to the extent of coordinates.

THE FUNCTION:

  function zoomBE(search_result){
    var coordinates = [];
    for(var i = 0; i <search_result.length; i++){
      coordinates.push(search_result[i].geometry.coordinates);
    }
    var boundingExtent = ol.extent.boundingExtent(coordinates);  
    view.fit(boundingExtent, map.getSize()); 
  }

I checked the boundingExtent and it is a valid array, like: [9.081426, 45.80251, 9.081426, 45.80251]

map.getSize() also returns valid.

What happens here is that it zooms to [0.0001, 0.0004].

Any ideas on what can be wrong? (OpenLayers version is 3.13.0)

Best Answer

Possibly same issue like in OpenLayers 3 create bounding extent which includes two points

Presumably your map is in 3857 projection, and therefore you have to transform your extent:

var boundingExtent = ol.extent.boundingExtent(coordinates);  
boundingExtent = ol.proj.transformExtent(boundingExtent, ol.proj.get('EPSG:4326'), ol.proj.get('EPSG:3857'));
view.fit(boundingExtent, map.getSize());