ZoomToExtent OpenLayers 3

Tyler DeWitt picture Tyler DeWitt · May 15, 2014 · Viewed 25.7k times · Source

OpenLayers 2 had a very useful map.zoomToExtent(extent) feature. Is there something similar in OpenLayers 3? I can get the extent of interest with source.getExtent(), but I can't figure out how to apply that extent as a "zoom level".

Answer

Tyler DeWitt picture Tyler DeWitt · May 16, 2014

Going off the function sfletche linked to:

var extent = source.getExtent();
map.getView().fitExtent(extent, map.getSize());

EDIT July 23, 2013

Apparently fitExtent is deprecated. Should be ol.View.fit, so something linke this (untestesd):

var extent = source.getExtent();
map.getView().fit(extent, map.getSize());