If I run the following code in OpenLayers 3...

map.getView().fit(map.getView().calculateExtent(map.getSize()), map.getSize());

I get the following output logged...


In my project, I would like to be able to store the current extent, so that I can reuse it later and go back to exactly the same extent. But it doesn't work as I expect it to.

It appears to be zooming out an entire zoom level.

Why is it behaving this way, and how can it get it to go back to the same extent that I had before?


Pass in options to the fit function, specifically constrainResolution false

  • I’ll give this a try when I get the chance. The documentation on this option merely says “constrain the resolution”. What does this option actually do? Could you please provide some more explanation in your answer. – Son of a Beach Jul 31 '18 at 1:23
  • It will match to one of the resolutions of the view, which will mean that your extent will not exactly match the given extent – bartvde Jul 31 '18 at 6:30
  • This is now working correctly for me. Thank you. What's embarrassing is that I should have thought of this. The 'constrainResolution' option was introduced for the 'PinchZoom' handler explicitly in response to my first OL question on this site (and on Stack Overflow)! See: gis.stackexchange.com/questions/220354/… – Son of a Beach Jul 31 '18 at 7:27

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.