ol. ol.Map

The map is the core component of OpenLayers. In its minimal configuration it needs a view, one or more layers, and a target container:

var map = new ol.Map({
  view: new ol.View2D({
    center: [0, 0],
    zoom: 1
  }),
  layers: [
    new ol.layer.Tile({
      source: new ol.source.MapQuestOSM()
    })
  ],
  target: 'map'
});

The above snippet creates a map with a MapQuest OSM layer on a 2D view and renders it to a DOM element with the id map.

new Map

Stability: experimental
Parameters:
Name Type Description
options olx.MapOptions

Map options.

Source:
  • map.js, line 168

Extends

Observable Properties

Name Type Settable Event Description
layergroup ol.layer.LayerGroup yes change:layergroup a layer group containing the layers in this map.
size ol.Size yes change:size the size in pixels of the map in the DOM
target string | Element yes change:target the Element or id of the Element that the map is rendered in.
view ol.IView yes change:view the view that controls this map

Methods

addControl

Stability: experimental

Add the given control to the map.

Parameters:
Name Type Description
control ol.control.Control

Control.

Source:
  • map.js, line 460

addInteraction

Stability: not documented

Add the given interaction to the map.

Parameters:
Name Type Description
interaction ol.interaction.Interaction

Interaction to add.

Source:
  • map.js, line 471

addLayer

Stability: experimental

Adds the given layer to the top of this map.

Parameters:
Name Type Description
layer ol.layer.Base

Layer.

Source:
  • map.js, line 483

addOverlay

Stability: experimental

Add the given overlay to the map.

Parameters:
Name Type Description
overlay ol.Overlay

Overlay.

Source:
  • map.js, line 495

beforeRender

Stability: experimental

Add functions to be called before rendering. This can be used for attaching animations before updating the map's view. The ol.animation namespace provides several static methods for creating prerender functions.

Parameters:
Name Type Description
var_args ol.PreRenderFunction

Any number of pre-render functions.

Source:
  • map.js, line 509

bindTo

Stability: experimental

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;
    }
  );
Parameters:
Name Type Argument Description
key string

Key name.

target ol.Object

Target.

targetKey string <optional>

Target key.

Inherited From:
Source:
  • object.js, line 232
Returns:
Type
ol.ObjectAccessor

dispatchChangeEvent

Stability: experimental
Inherited From:
Source:
  • observable.js, line 35

forEachFeatureAtPixel

Stability: not documented
Parameters:
Name Type Argument Description
pixel ol.Pixel

Pixel.

callback function

Feature callback.

this S <optional>

Value to use as this when executing callback.

layerFilter function

Layer filter function, only layers which are visible and for which this function returns true will be tested for features. By default, all visible layers will be tested.

this2 U <optional>

Value to use as this when executing layerFilter.

Source:
  • map.js, line 547
Returns:

Callback result.

Type
T | undefined

get

Stability: experimental

Gets a value.

Parameters:
Name Type Description
key string

Key name.

Inherited From:
Source:
  • object.js, line 289
Returns:

Value.

Type
*

getControls

Stability: experimental
Source:
  • map.js, line 636
Returns:

Controls.

Type
ol.Collection

getCoordinateFromPixel

Stability: not documented
Parameters:
Name Type Description
pixel ol.Pixel

Pixel.

Source:
  • map.js, line 621
Returns:

Coordinate.

Type
ol.Coordinate

getEventCoordinate

Stability: experimental

Returns the geographical coordinate for a browser event.

Parameters:
Name Type Description
event Event

Event.

Source:
  • map.js, line 569
Returns:

Coordinate.

Type
ol.Coordinate

getEventPixel

Stability: experimental

Returns the map pixel position for a browser event.

Parameters:
Name Type Description
event Event

Event.

Source:
  • map.js, line 580
Returns:

Pixel.

Type
ol.Pixel

getInteractions

Stability: experimental

Gets the collection of ol.interaction.Interaction instances associated with this map. Modifying this collection changes the interactions associated with the map.

Interactions are used for e.g. pan, zoom and rotate.

Source:
  • map.js, line 660
Returns:

Interactions.

Type
ol.Collection

getLayerGroup

Stability: experimental

Get the layergroup associated with this map.

Source:
  • map.js, line 670
Returns:

LayerGroup.

Type
ol.layer.Group

getLayers

Stability: experimental

Get the collection of layers associated with this map.

Source:
  • map.js, line 685
Returns:

Layers.

Type
ol.Collection | undefined

getOverlays

Stability: experimental
Source:
  • map.js, line 645
Returns:

Overlays.

Type
ol.Collection

getPixelFromCoordinate

Stability: not documented
Parameters:
Name Type Description
coordinate ol.Coordinate

Coordinate.

Source:
  • map.js, line 699
Returns:

Pixel.

Type
ol.Pixel

getProperties

Stability: experimental

Get an object of all property names and values.

Inherited From:
Source:
  • object.js, line 348
Returns:

Object.

Type
Object.<string, *>

getSize

Stability: experimental

Get the size of this map.

Source:
  • map.js, line 715
Returns:

Size.

Type
ol.Size | undefined

getTarget

Stability: experimental

Get the target in which this map is rendered. Note that this returns what is entered as an option or in setTarget: if that was an element, it returns an element; if a string, it returns that.

Source:
  • map.js, line 607
Returns:

Target.

Type
Element | string | undefined

getView

Stability: experimental

Get the view associated with this map. This can be a 2D or 3D view. A 2D view manages properties such as center and resolution.

Source:
  • map.js, line 730
Returns:

View.

Type
ol.View | undefined

getViewport

Stability: experimental
Source:
  • map.js, line 743
Returns:

Viewport.

Type
Element

notify

Stability: experimental

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.

Parameters:
Name Type Description
key string

Key name.

Inherited From:
Source:
  • object.js, line 368

on

Stability: experimental

Listen for a certain type of event.

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

The event type or array of event types.

listener function

The listener function.

this Object <optional>

The object to use as this in listener.

Inherited From:
Source:
  • observable.js, line 58
Returns:

Unique key for the listener.

Type
goog.events.Key

once

Stability: experimental

Listen once for a certain type of event.

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

The event type or array of event types.

listener function

The listener function.

this Object <optional>

The object to use as this in listener.

Inherited From:
Source:
  • observable.js, line 71
Returns:

Unique key for the listener.

Type
goog.events.Key

removeControl

Stability: experimental

Remove the given control from the map.

Parameters:
Name Type Description
control ol.control.Control

Control.

Source:
  • map.js, line 1072
Returns:

The removed control of undefined if the control was not found.

Type
ol.control.Control | undefined

removeInteraction

Stability: not documented

Remove the given interaction from the map.

Parameters:
Name Type Description
interaction ol.interaction.Interaction

Interaction to remove.

Source:
  • map.js, line 1088
Returns:

The removed interaction (or undefined if the interaction was not found).

Type
ol.interaction.Interaction | undefined

removeLayer

Stability: experimental

Removes the given layer from the map.

Parameters:
Name Type Description
layer ol.layer.Base

Layer.

Source:
  • map.js, line 1106
Returns:

The removed layer or undefined if the layer was not found.

Type
ol.layer.Base | undefined

removeOverlay

Stability: experimental

Remove the given overlay from the map.

Parameters:
Name Type Description
overlay ol.Overlay

Overlay.

Source:
  • map.js, line 1120
Returns:

The removed overlay of undefined if the overlay was not found.

Type
ol.Overlay | undefined

render

Stability: not documented

Request that renderFrame_ be called some time in the future.

Source:
  • map.js, line 1058

renderSync

Stability: not documented

Render.

Source:
  • map.js, line 1050

set

Stability: experimental

Sets a value.

Parameters:
Name Type Description
key string

Key name.

value *

Value.

Inherited From:
Source:
  • object.js, line 399

setLayerGroup

Stability: experimental

Sets the layergroup of this map.

Parameters:
Name Type Description
layerGroup ol.layer.Group

Layergroup.

Source:
  • map.js, line 1245

setSize

Stability: experimental

Set the size of this map.

Parameters:
Name Type Description
size ol.Size | undefined

Size.

Source:
  • map.js, line 1259

setTarget

Stability: experimental

Set the target element to render this map into.

Parameters:
Name Type Description
target Element | string | undefined

Target.

Source:
  • map.js, line 1273

setValues

Stability: experimental

Sets a collection of key-value pairs.

Parameters:
Name Type Description
values Object.<string, *>

Values.

Inherited From:
Source:
  • object.js, line 428

setView

Stability: experimental

Set the view for this map.

Parameters:
Name Type Description
view ol.IView

View.

Source:
  • map.js, line 1287

un

Stability: experimental

Unlisten for a certain type of event.

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

The event type or array of event types.

listener function

The listener function.

this Object <optional>

The object to use as this in listener.

Inherited From:
Source:
  • observable.js, line 83

unbind

Stability: experimental

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.

Parameters:
Name Type Description
key string

Key name.

Inherited From:
Source:
  • object.js, line 442

unbindAll

Stability: experimental

Removes all bindings.

Inherited From:
Source:
  • object.js, line 466

unByKey

Stability: experimental

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

Parameters:
Name Type Description
key goog.events.Key

Key.

Inherited From:
Source:
  • observable.js, line 93

updateSize

Stability: experimental

Force a recalculation of the map viewport size. This should be called when third-party code changes the size of the map viewport.

Source:
  • map.js, line 1301