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

Stability: experimental
Parameters:
Name Type Argument Description
options olx.View2DOptions <optional>

View2D options.

Source:
  • view2d.js, line 95

Observable Properties

Name Type Settable Event Description
center ol.Coordinate yes change:center the center of the view
projection ol.proj.Projection yes change:projection the projection of the view
resolution number yes change:resolution the resolution of the view
rotation number yes change:rotation the rotation of the view

Methods

calculateExtent

Stability: experimental

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 242
Returns:

Extent.

Type
ol.Extent

constrainResolution

Stability: experimental

Get the constrained the resolution of this view.

Parameters:
Name Type Argument Description
resolution number | undefined

Resolution.

delta number <optional>

Delta. Default is 0.

direction number <optional>

Direction. Default is 0.

Source:
  • view2d.js, line 201
Returns:

Constrained resolution.

Type
number | undefined

constrainRotation

Stability: experimental

Get the constrained rotation of this view.

Parameters:
Name Type Argument Description
rotation number | undefined

Rotation.

delta number <optional>

Delta. Default is 0.

Source:
  • view2d.js, line 216
Returns:

Constrained rotation.

Type
number | undefined

fitExtent

Stability: experimental

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 419

getZoom

Stability: experimental

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

Source:
  • view2d.js, line 393
Returns:

Zoom.

Type
number | undefined

setCenter

Stability: experimental

Set the center of the current view.

Parameters:
Name Type Description
center ol.Coordinate | undefined

Center.

Source:
  • view2d.js, line 447

setProjection

Stability: experimental

Set the projection of this view. Warning! This code is not yet implemented. Function should not be used.

Parameters:
Name Type Description
projection ol.proj.Projection | undefined

Projection.

Source:
  • view2d.js, line 462

setResolution

Stability: experimental

Set the resolution for this view.

Parameters:
Name Type Description
resolution number | undefined

Resolution.

Source:
  • view2d.js, line 476

setRotation

Stability: experimental

Set the rotation for this view.

Parameters:
Name Type Description
rotation number | undefined

Rotation.

Source:
  • view2d.js, line 490

setZoom

Stability: experimental

Zoom to a specific zoom level.

Parameters:
Name Type Description
zoom number

Zoom level.

Source:
  • view2d.js, line 504