Constraining a view

There are some cases when restricting the center is not enough; the users should not pan outside an arbitrary extent. There is an easy method to do this; however, it is hard to calculate with rotations and impossible to show every part of a rotated map with a hard constrained view. In the next example, called ch06_constrain, we will implement this method.

Note

We will learn how to rotate the map later in this chapter.

An easy implementation calculates a new extent based on a more permissive, full extent for a given resolution. Then, we simply check the value of the center in every change in the map's view and snap it to our new extent's border if it tries to escape it:

map.getView().on('propertychange', function (evt) { var projExtent ...

Get Mastering OpenLayers 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.