Class: View2D

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.

experimental new ol.View2D(opt_options)

Name Type Argument Description
options optional

View2D options.

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.

Fires:

Extends

Observable Properties

Name Type Settable ol.ObjectEvent type Description
center ol.Coordinate | undefined yes change:center

The center of the view.

projection ol.proj.Projection | undefined yes change:projection

The projection of the view.

resolution number | undefined yes change:resolution

The resolution of the view.

rotation number | undefined yes change:rotation

The rotation of the view.

Methods

experimental inherited bindTo(key, target, opt_targetKey){ol.ObjectAccessor}

The bindTo method allows you to set up a two-way binding between a source and target object. The method returns an ol.ObjectAccessor with a transform method that lets you transform values on the way from the source to the target and on the way back.

For example, if you had two map views (sourceView and targetView) and you wanted the target view to have double the resolution of the source view, you could transform the resolution on the way to and from the target with the following:

sourceView.bindTo('resolution', targetView)
  .transform(
    function(sourceResolution) {
      // from sourceView.resolution to targetView.resolution
      return 2 * sourceResolution;
    },
    function(targetResolution) {
      // from targetView.resolution to sourceView.resolution
      return targetResolution / 2;
    }
  );
Name Type Argument Description
key string

Key name.

target ol.Object

Target.

targetKey string <optional>
optional

Target key.

experimental calculateExtent(size){ol.Extent}

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

Name Type Description
size ol.Size

Box pixel size.

experimental centerOn(coordinate, size, position)

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

Name Type Description
coordinate ol.Coordinate

Coordinate.

size ol.Size

Box pixel size.

position ol.Pixel

Position on the view to center on.

experimental constrainResolution(resolution, opt_delta, opt_direction){number|undefined}

Get the constrained the resolution of this view.

Name Type Argument Description
resolution number | undefined

Resolution.

delta number <optional>
optional

Delta. Default is 0.

direction number <optional>
optional

Direction. Default is 0.

Returns:
resolution.

experimental constrainRotation(rotation, opt_delta){number|undefined}

Get the constrained rotation of this view.

Name Type Argument Description
rotation number | undefined

Rotation.

delta number <optional>
optional

Delta. Default is 0.

Returns:
rotation.

experimental inherited dispatchChangeEvent()

Dispatches a change event. Register a listener for this event to get notified of changes.

Fires:
  • change

experimental fitExtent(extent, size)

Fit the given extent based on the given map size.

Name Type Description
extent ol.Extent

Extent.

size ol.Size

Box pixel size.

experimental fitGeometry(geometry, size, opt_options)

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

Name Type Argument Description
geometry ol.geom.SimpleGeometry

Geometry.

size ol.Size

Box pixel size.

options optional

Options.

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.

experimental inherited get(key){*}

Gets a value.

Name Type Description
key string

Key name.

experimental getCenter(){ol.Coordinate|undefined}

Returns:
center of the view.

experimental inherited getKeys(){Array.<string>}

Get a list of object property names.

Returns:
of property names.

experimental getProjection(){ol.proj.Projection|undefined}

Returns:
projection of the view.

experimental inherited getProperties(){Object.<string, *>}

Get an object of all property names and values.

experimental getResolution(){number|undefined}

Returns:
resolution of the view.

experimental getRotation(){number|undefined}

Returns:
rotation of the view.

experimental getView2D(){ol.IView2D}

experimental getZoom(){number|undefined}

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

experimental inherited notify(key)

Notify all observers of a change on this property. This notifies both objects that are bound to the object's property as well as the object that it is bound to.

Name Type Description
key string

Key name.

experimental inherited on(type, listener, opt_this){goog.events.Key}

Listen for a certain type of event.

Name Type Argument Description
type string | Array.<string>

The event type or array of event types.

listener function

The listener function.

this Object <optional>
optional

The object to use as this in listener.

Returns:
key for the listener.

experimental inherited once(type, listener, opt_this){goog.events.Key}

Listen once for a certain type of event.

Name Type Argument Description
type string | Array.<string>

The event type or array of event types.

listener function

The listener function.

this Object <optional>
optional

The object to use as this in listener.

Returns:
key for the listener.

experimental rotate(rotation, opt_anchor)

Rotate the view around a given coordinate.

Name Type Argument Description
rotation number

New rotation value for the view.

anchor ol.Coordinate <optional>
optional

The rotation center.

experimental inherited set(key, value)

Sets a value.

Name Type Description
key string

Key name.

value *

Value.

experimental setCenter(center)

Set the center of the current view.

Name Type Description
center ol.Coordinate | undefined

The center of the view.

experimental setProjection(projection)

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

Name Type Description
projection ol.proj.Projection | undefined

The projection of the view.

experimental setResolution(resolution)

Set the resolution for this view.

Name Type Description
resolution number | undefined

The resolution of the view.

experimental setRotation(rotation)

Set the rotation for this view.

Name Type Description
rotation number | undefined

The rotation of the view.

experimental inherited setValues(values)

Sets a collection of key-value pairs.

Name Type Description
values Object.<string, *>

Values.

experimental setZoom(zoom)

Zoom to a specific zoom level.

Name Type Description
zoom number

Zoom level.

experimental inherited un(type, listener, opt_this)

Unlisten for a certain type of event.

Name Type Argument Description
type string | Array.<string>

The event type or array of event types.

listener function

The listener function.

this Object <optional>
optional

The object to use as this in listener.

experimental inherited unbind(key)

Removes a binding. Unbinding will set the unbound property to the current value. The object will not be notified, as the value has not changed.

Name Type Description
key string

Key name.

experimental inherited unbindAll()

Removes all bindings.

experimental inherited unByKey(key)

Removes an event listener using the key returned by on() or once().

Name Type Description
key goog.events.Key

Key.