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.

Properties
Name Type Description
center ol.Coordinate | undefined

The initial center for the view. The coordinate system for the center is specified with the projection option. Default is undefined, and layer sources will not be fetched if this is not set.

constrainRotation boolean | number | undefined

Rotation constraint. false means no constraint. true means no constraint, but snap to zero near zero. A number constraints the rotation to that number of values. For example, 4 will constrain the rotation to 0, 90, 180, and 270 degrees. The default is true.

enableRotation boolean | undefined

Enable rotation. Default is true.

extent ol.Extent | undefined

The extent that constrains the center, in other words, center cannot be set outside this extent. Default is undefined.

maxResolution number | undefined

The maximum resolution used to determine the resolution constraint. It is used together with maxZoom and zoomFactor. If unspecified it is calculated in such a way that the projection's validity extent fits in a 256x256 px tile. If the projection is Spherical Mercator (the default) then maxResolution defaults to 40075016.68557849 / 256 = 156543.03392804097.

maxZoom number | undefined

The maximum zoom level used to determine the resolution constraint. It is used together with maxResolution and zoomFactor. Default is 28.

projection ol.proj.ProjectionLike

The projection. Default is EPSG:3857 (Spherical Mercator).

resolution number | undefined

The initial resolution for the view. The units are projection units per pixel (e.g. meters per pixel). An alternative to setting this is to set zoom. Default is undefined, and layer sources will not be fetched if neither this nor zoom are defined.

resolutions Array.<number> | undefined

Resolutions to determine the resolution constraint. If set the maxResolution, maxZoom and zoomFactor options are ignored.

rotation number | undefined

The initial rotation for the view in radians (positive rotation clockwise). Default is 0.

zoom number | undefined

Only used if resolution is not defined. Zoom level used to calculate the initial resolution for the view. The initial resolution is determined using the ol.View2D#constrainResolution method.

zoomFactor number | undefined

The zoom factor used to determine the resolution constraint. Used together with maxResolution and maxZoom. Default is 2.

Source:
  • view2d.js, line 95
Fires:

Extends

  • ol.IView2D (Interface)
  • ol.IView3D (Interface)
  • ol.View

Observable Properties

Name Type Settable ol.ObjectEvent type 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 243
Returns:

Extent.

Type
ol.Extent

centerOn

Stability: experimental

Center on coordinate and view position. Take care on the map angle.

Parameters:
Name Type Description
coordinate ol.Coordinate

Coordinate.

size ol.Size

Box pixel size.

position ol.Pixel

Position on the view to center on.

Source:
  • view2d.js, line 513

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 424

fitGeometry

Stability: experimental

Fit the given geometry based on the given map size and border. Take care on the map angle.

Parameters:
Name Type Argument Description
geometry ol.geom.SimpleGeometry

Geometry.

size ol.Size

Box pixel size.

options olx.View2D.fitGeometryOptions <optional>

Options.

Properties
Name Type Description
padding !Array.<number>

Padding (in pixels) to be cleared inside the view. Values in the array are top, right, bottom and left padding. Default is [0, 0, 0, 0].

constrainResolution boolean | undefined

Constrain the resolution. Default is true.

nearest boolean | undefined

Get the nearest extent. Default is false.

minResolution number | undefined

Minimum resolution that we zoom to. Default is 0.

Source:
  • view2d.js, line 446

getCenter

Stability: experimental
Source:
  • view2d.js, line 226
Returns:

Map center.

Type
ol.Coordinate | undefined

getProjection

Stability: experimental
Source:
  • view2d.js, line 259
Returns:

Map projection.

Type
ol.proj.Projection | undefined

getResolution

Stability: experimental
Source:
  • view2d.js, line 273
Returns:

Map resolution.

Type
number | undefined

getRotation

Stability: experimental
Source:
  • view2d.js, line 324
Returns:

Map rotation.

Type
number | undefined

getView2D

Stability: experimental
Source:
  • view2d.js, line 362
Returns:

View2D.

Type
ol.IView2D

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

Zoom.

Type
number | undefined

rotate

Stability: experimental

Rotate the view around a given coordinate.

Parameters:
Name Type Argument Description
rotation number

New rotation value for the view.

anchor ol.Coordinate <optional>

The rotation center.

Source:
  • view2d.js, line 548

setCenter

Stability: experimental

Set the center of the current view.

Parameters:
Name Type Description
center ol.Coordinate | undefined

Center.

Source:
  • view2d.js, line 562

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 577

setResolution

Stability: experimental

Set the resolution for this view.

Parameters:
Name Type Description
resolution number | undefined

Resolution.

Source:
  • view2d.js, line 591

setRotation

Stability: experimental

Set the rotation for this view.

Parameters:
Name Type Description
rotation number | undefined

Rotation.

Source:
  • view2d.js, line 605

setZoom

Stability: experimental

Zoom to a specific zoom level.

Parameters:
Name Type Description
zoom number

Zoom level.

Source:
  • view2d.js, line 619