ol. ol.View2D

An ol.View2D object represents a simple 2D view of the map.

This is the object to act upon to change the center, resolution, and rotation of the map.

The view states

An ol.View2D is determined by three states: center, resolution, and rotation. To each state corresponds a getter and a setter. E.g. getCenter and setCenter for the center state.

An ol.View2D has a projection. The projection determines the coordinate system of the center, and its units determine the units of the resolution (projection units per pixel). The default projection is Spherical Mercator (EPSG:3857).

The constraints

setCenter, setResolution and setRotation can be used to change the states of the view. Any value can be passed to the setters. And the value that is passed to a setter will effectively be the value set in the view, and returned by the corresponding getter.

But an ol.View2D object also has a resolution constraint and a rotation constraint. There's currently no center constraint, but this may change in the future.

As said above no constraints are applied when the setters are used to set new states for the view. Applying constraints is done explicitly through the use of the constrain* functions (constrainResolution and constrainRotation).

The main users of the constraints are the interactions and the controls. For example, double-clicking on the map changes the view to the "next" resolution. And releasing the fingers after pinch-zooming snaps to the closest resolution (with an animation).

So the resolution constraint snaps to specific resolutions. It is determined by the following options: resolutions, maxResolution, maxZoom, and zoomFactor. If resolutions is set, the other three options are ignored. See ol.View2DOptions for more information.

The rotation constaint is currently not configurable. It snaps the rotation value to zero when approaching the horizontal.

new View2D

Parameters:
Name Type Argument Description
options ol.View2DOptions <optional>
(Optional)

View2D options.

Source:
  • view2d.js, line 87

Methods

calculateExtent

Calculate the extent for the given size in pixels, based on the current resolution and the current center.

Parameters:
Name Type Description
size ol.Size

Box pixel size.

Source:
  • view2d.js, line 221
Returns:

Extent.

Type
ol.Extent

constrainResolution

Get the constrained the resolution of this view.

Parameters:
Name Type Argument Description
resolution number | undefined

Resolution.

delta number <optional>
(Optional)

Delta.

direction number <optional>
(Optional)

Direction.

Source:
  • view2d.js, line 182
Returns:

Constrained resolution.

Type
number | undefined

constrainRotation

Get the constrained rotation of this view.

Parameters:
Name Type Argument Description
rotation number | undefined

Rotation.

delta number <optional>
(Optional)

Delta.

Source:
  • view2d.js, line 196
Returns:

Constrained rotation.

Type
number | undefined

fitExtent

Fit the given extent based on the given map size.

Parameters:
Name Type Description
extent ol.Extent

Extent.

size ol.Size

Box pixel size.

Source:
  • view2d.js, line 396

getZoom

Get the current zoom level. Return undefined if the current resolution is undefined or not a "constrained resolution".

Source:
  • view2d.js, line 371
Returns:

Zoom.

Type
number | undefined

setCenter

Set the center of the current view.

Parameters:
Name Type Description
center ol.Coordinate | undefined

Center.

Source:
  • view2d.js, line 417

setProjection

Set the projection of this view.

Parameters:
Name Type Description
projection ol.Projection | undefined

Projection.

Source:
  • view2d.js, line 430

setResolution

Set the resolution for this view.

Parameters:
Name Type Description
resolution number | undefined

Resolution.

Source:
  • view2d.js, line 443

setRotation

Set the rotation for this view.

Parameters:
Name Type Description
rotation number | undefined

Rotation.

Source:
  • view2d.js, line 456

setZoom

Zoom to a specific zoom level.

Parameters:
Name Type Description
zoom number

Zoom level.

Source:
  • view2d.js, line 469