2

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)

3

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()); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.