OpenLayers.Map

Instances of OpenLayers.Map are interactive maps embedded in a web page.  Create a new map with the OpenLayers.Map constructor.

On their own maps do not provide much functionality.  To extend a map it’s necessary to add controls (OpenLayers.Control) and layers (OpenLayers.Layer) to the map.

Summary
Instances of OpenLayers.Map are interactive maps embedded in a web page.
{Object} Base z-indexes for different classes of thing
{Array(String)} supported application event types
{String} Unique identifier for the map
{OpenLayers.Events} An events object that handles all events on the map
{DOMElement} The element that contains the map
{OpenLayers.Size} Size of the main div (this.div)
{HTMLDivElement} The element that represents the map viewport
{OpenLayers.LonLat} The lonlat at which the later container was re-initialized (on-zoom)
{HTMLDivElement} The element that contains the layers.
{Array(OpenLayers.Layer)} Ordered list of layers in the map
{Array(OpenLayers.Control)} List of controls associated with the map
{Array(OpenLayers.Popup)} List of popups associated with the map
{OpenLayers.Layer} The currently selected base layer.
{OpenLayers.LonLat} The current center of the map
{Integer} The current zoom level of the map
{String} Used to store a unique identifier that changes when the map view changes.
{OpenLayers.Size} Set in the map options to override the default tile size for this map.
{String} Set in the map options to override the default projection string this map - also set maxExtent, maxResolution, and units if appropriate.
{String} The map units.
{Array(Float)} A list of map resolutions (map units per pixel) in descending order.
{Float} Default max is 360 deg / 256 px, which corresponds to zoom level 0 on gmaps.
{Float}
{Float}
{Float}
{OpenLayers.Bounds} The maximum extent for the map.
{OpenLayers.Bounds} Limit map navigation to this extent where possible.
{Integer} Number of zoom levels for the map.
{String} Relative path to a CSS file from which to load theme styles.
{Boolean} Should OpenLayers allow events on the map to fall through to other elements on the page, or should it swallow them? 
Constructor for a new OpenLayers.Map instance.
Function that is called to destroy the map on page unload.
Destroy this map
Change the map options
Get the tile size for the map
Get a layer based on its id
Removes a layer from the map by removing its visual element (the layer.div property), then removing it from the map’s internal list of layers, setting the layer’s map property to null.
{Int} The number of layers attached to the map.
Move the given layer to the specified (zero-based) index in the layer list, changing its z-index in the map display.
Change the index of the given layer by delta.
Allows user to specify one of the currently-loaded layers as the Map’s new base layer.
Remove a control from the map.
{OpenLayers.Size} An OpenLayers.Size object that represents the size, in pixels, of the div into which OpenLayers has been loaded.
This function should be called by any external code which dynamically changes the size of the map div (because mozilla wont let us catch the “onresize” for an element)
{OpenLayers.Size} A new OpenLayers.Size object with the dimensions of the map div
{Integer}
Allows user to pan by a value of screen pixels
This function takes care to recenter the layerContainerDiv.
{String} The Projection of the base layer.
{String} The Map’s Maximum Resolution
{Integer} The total number of zoom levels that can be displayed by the current baseLayer.
{OpenLayers.Bounds} A Bounds object which represents the lon/lat bounds of the current viewPort.
{Float} The current resolution of the map.
{Float} The current scale denominator of the map.
Zoom to a specific zoom level
Zoom to the passed in bounds, recenter
Zoom to the full extent and recenter.
Zoom to a specified scale
{Integer} 256 Default tile width (unless otherwise specified)
{Integer} 256 Default tile height (unless otherwise specified)

Constants

Z_INDEX_BASE

{Object} Base z-indexes for different classes of thing

EVENT_TYPES

{Array(String)} supported application event types

Properties

id

{String} Unique identifier for the map

events

{OpenLayers.Events} An events object that handles all events on the map

div

{DOMElement} The element that contains the map

size

{OpenLayers.Size} Size of the main div (this.div)

viewPortDiv

{HTMLDivElement} The element that represents the map viewport

layerContainerOrigin

{OpenLayers.LonLat} The lonlat at which the later container was re-initialized (on-zoom)

layerContainerDiv

{HTMLDivElement} The element that contains the layers.

layers

{Array(OpenLayers.Layer)} Ordered list of layers in the map

controls

{Array(OpenLayers.Control)} List of controls associated with the map

popups

{Array(OpenLayers.Popup)} List of popups associated with the map

baseLayer

{OpenLayers.Layer} The currently selected base layer.  This determines min/max zoom level, projection, etc.

center

{OpenLayers.LonLat} The current center of the map

zoom

{Integer} The current zoom level of the map

viewRequestID

{String} Used to store a unique identifier that changes when the map view changes. viewRequestID should be used when adding data asynchronously to the map: viewRequestID is incremented when you initiate your request (right now during changing of baselayers and changing of zooms).  It is stored here in the map and also in the data that will be coming back asynchronously.  Before displaying this data on request completion, we check that the viewRequestID of the data is still the same as that of the map.  Fix for #480

tileSize

{OpenLayers.Size} Set in the map options to override the default tile size for this map.

projection

{String} Set in the map options to override the default projection string this map - also set maxExtent, maxResolution, and units if appropriate.

units

{String} The map units.  Defaults to ‘degrees’.  Possible values are ‘degrees’ (or ‘dd’), ‘m’, ‘ft’, ‘km’, ‘mi’, ‘inches’.

resolutions

{Array(Float)} A list of map resolutions (map units per pixel) in descending order.  If this is not set in the layer constructor, it will be set based on other resolution related properties (maxExtent, maxResolution, maxScale, etc.).

maxResolution

{Float} Default max is 360 deg / 256 px, which corresponds to zoom level 0 on gmaps.  Specify a different value in the map options if you are not using a geographic projection and displaying the whole world.

minResolution

{Float}

maxScale

{Float}

minScale

{Float}

maxExtent

{OpenLayers.Bounds} The maximum extent for the map.  Defaults to the whole world in decimal degrees (-180, -90, 180, 90).  Specify a different extent in the map options if you are not using a geographic projection and displaying the whole world.

restrictedExtent

{OpenLayers.Bounds} Limit map navigation to this extent where possible.  If a non-null restrictedExtent is set, panning will be restricted to the given bounds.  In addition, zooming to a resolution that displays more than the restricted extent will center the map on the restricted extent.  If you wish to limit the zoom level or resolution, use maxResolution.

numZoomLevels

{Integer} Number of zoom levels for the map.  Defaults to 16.  Set a different value in the map options if needed.

theme

{String} Relative path to a CSS file from which to load theme styles.  Specify null in the map options (e.g.  {theme: null}) if you want to get cascading style declarations - by putting links to stylesheets or style declarations directly in your page.

fallThrough

{Boolean} Should OpenLayers allow events on the map to fall through to other elements on the page, or should it swallow them?  (#457) Default is to swallow them.

Constructor

OpenLayers.Map

Constructor for a new OpenLayers.Map instance.

Parameters

div{String} Id of an element in your page that will contain the map.
options{Object} Optional object with properties to tag onto the map.

Examples

// create a map with default options in an element with the id "map1"
var map = new OpenLayers.Map("map1");

// create a map with non-default options in an element with id "map2"
var options = {
maxExtent: new OpenLayers.Bounds(-200000, -200000, 200000, 200000),
maxResolution: 156543,
units: 'meters',
projection: "EPSG:41001"
};
var map = new OpenLayers.Map("map2", options);

Functions

unloadDestroy

Function that is called to destroy the map on page unload. stored here so that if map is manually destroyed, we can unregister this.

destroy

destroy:function()

Destroy this map

setOptions

setOptions: function(options)

Change the map options

Parameters

options{Object} Hashtable of options to tag to the map

getTileSize

getTileSize: function()

Get the tile size for the map

Returns

{OpenLayers.Size}

getLayer

getLayer: function(id)

Get a layer based on its id

Parameter

id{String} A layer id

Returns

{OpenLayers.Layer} The Layer with the corresponding id from the map’s layer collection, or null if not found.

setLayerZIndex

setLayerZIndex: function (layer,
zIdx)

Parameters

layer{OpenLayers.Layer}
zIdx{int}

addLayer

addLayer: function (layer)

Parameters

layer{OpenLayers.Layer}

addLayers

addLayers: function (layers)

Parameters

layersArray({OpenLayers.Layer})

removeLayer

removeLayer: function(layer,
setNewBaseLayer)

Removes a layer from the map by removing its visual element (the layer.div property), then removing it from the map’s internal list of layers, setting the layer’s map property to null.

a “removelayer” event is triggered.

very worthy of mention is that simply removing a layer from a map will not cause the removal of any popups which may have been created by the layer. this is due to the fact that it was decided at some point that popups would not belong to layers. thus there is no way for us to know here to which layer the popup belongs.

A simple solution to this is simply to call destroy() on the layer. the default OpenLayers.Layer class’s destroy() function automatically takes care to remove itself from whatever map it has been attached to.

The correct solution is for the layer itself to register an event-handler on “removelayer” and when it is called, if it recognizes itself as the layer being removed, then it cycles through its own personal list of popups, removing them from the map.

Parameters

layer{OpenLayers.Layer}
setNewBaseLayer{Boolean} Default is true

getNumLayers

getNumLayers: function ()

Returns

{Int} The number of layers attached to the map.

getLayerIndex

getLayerIndex: function (layer)

Parameters

layer{OpenLayers.Layer}

Returns

{Integer} The current (zero-based) index of the given layer in the map’s layer stack.  Returns -1 if the layer isn’t on the map.

setLayerIndex

setLayerIndex: function (layer,
idx)

Move the given layer to the specified (zero-based) index in the layer list, changing its z-index in the map display.  Use map.getLayerIndex() to find out the current index of a layer.  Note that this cannot (or at least should not) be effectively used to raise base layers above overlays.

Parameters

layer{OpenLayers.Layer}
idx{int}

raiseLayer

raiseLayer: function (layer,
delta)

Change the index of the given layer by delta.  If delta is positive, the layer is moved up the map’s layer stack; if delta is negative, the layer is moved down.  Again, note that this cannot (or at least should not) be effectively used to raise base layers above overlays.

Paremeters

layer{OpenLayers.Layer}
idx{int}

setBaseLayer

setBaseLayer: function(newBaseLayer)

Allows user to specify one of the currently-loaded layers as the Map’s new base layer.

Parameters

newBaseLayer{OpenLayers.Layer}

addControl

addControl: function (control,
px)

Parameters

control{OpenLayers.Control}
px{OpenLayers.Pixel}

addControlToMap

addControlToMap: function (control,
px)

Parameters

control{OpenLayers.Control}
px{OpenLayers.Pixel}

getControl

getControl: function (id)

Parameters

id{String} ID of the control to return.

Returns

{OpenLayers.Control} The control from the map’s list of controls which has a matching ‘id’.  If none found, returns null.

removeControl

removeControl: function (control)

Remove a control from the map.  Removes the control both from the map object’s internal array of controls, as well as from the map’s viewPort (assuming the control was not added outsideViewport)

Parameters

control{OpenLayers.Control} The control to remove.

addPopup

addPopup: function(popup,
exclusive)

Parameters

popup{OpenLayers.Popup}
exclusive{Boolean} If true, closes all other popups first

removePopup

removePopup: function(popup)

Parameters

popup{OpenLayers.Popup}

getSize

getSize: function ()

Returns

{OpenLayers.Size} An OpenLayers.Size object that represents the size, in pixels, of the div into which OpenLayers has been loaded.  Note - A clone() of this locally cached variable is returned, so as not to allow users to modify it.

updateSize

updateSize: function()

This function should be called by any external code which dynamically changes the size of the map div (because mozilla wont let us catch the “onresize” for an element)

getCurrentSize

getCurrentSize: function()

Returns

{OpenLayers.Size} A new OpenLayers.Size object with the dimensions of the map div

calculateBounds

calculateBounds: function(center,
resolution)

Parameters

center{OpenLayers.LonLat} Default is this.getCenter()
resolution{float} Default is this.getResolution()

Returns

{OpenLayers.Bounds} A bounds based on resolution, center, and current mapsize.

getCenter

getCenter: function ()

Returns

{OpenLayers.LonLat}

getZoom

getZoom: function ()

Returns

{Integer}

pan

pan: function(dx,
dy)

Allows user to pan by a value of screen pixels

Parameters

dx{Integer}
dy{Integer}

setCenter

setCenter: function (lonlat,
zoom,
dragging,
forceZoomChange)

Parameters

lonlat{OpenLayers.LonLat}
zoom{Integer}
dragging{Boolean} Specifies whether or not to trigger movestart/end events
forceZoomChange{Boolean} Specifies whether or not to trigger zoom change events (needed on baseLayer change)

TBD: reconsider forceZoomChange in 3.0

centerLayerContainer

centerLayerContainer: function (lonlat)

This function takes care to recenter the layerContainerDiv.

Parameters

lonlat{OpenLayers.LonLat}

isValidZoomLevel

isValidZoomLevel: function(zoomLevel)

Parameters

zoomLevel{Integer}

Returns

{Boolean} Whether or not the zoom level passed in is non-null and within the min/max range of zoom levels.

isValidLonLat

isValidLonLat: function(lonlat)

Parameters

lonlat{OpenLayers.LonLat}

Returns

{Boolean} Whether or not the lonlat passed in is non-null and within the maxExtent bounds

getProjection

getProjection: function()

Returns

{String} The Projection of the base layer.

getMaxResolution

getMaxResolution: function()

Returns

{String} The Map’s Maximum Resolution

getMaxExtent

getMaxExtent: function ()

Returns

{OpenLayers.Bounds}

getNumZoomLevels

getNumZoomLevels: function()

Returns

{Integer} The total number of zoom levels that can be displayed by the current baseLayer.

getExtent

getExtent: function ()

Returns

{OpenLayers.Bounds} A Bounds object which represents the lon/lat bounds of the current viewPort.  If no baselayer is set, returns null.

getResolution

getResolution: function ()

Returns

{Float} The current resolution of the map.  If no baselayer is set, returns null.

getScale

getScale: function ()

Returns

{Float} The current scale denominator of the map.  If no baselayer is set, returns null.

getZoomForExtent

getZoomForExtent: function (bounds,
closest)

Parameters

bounds{OpenLayers.Bounds}
closest{Boolean} Find the zoom level that most closely fits the specified bounds.  Note that this may result in a zoom that does not exactly contain the entire extent.  Default is false.

Returns

{Integer} A suitable zoom level for the specified bounds.  If no baselayer is set, returns null.

getZoomForResolution

getZoomForResolution: function(resolution,
closest)

Parameter

resolution{Float}
closest{Boolean} Find the zoom level that corresponds to the absolute closest resolution, which may result in a zoom whose corresponding resolution is actually smaller than we would have desired (if this is being called from a getZoomForExtent() call, then this means that the returned zoom index might not actually contain the entire extent specified... but it’ll be close).  Default is false.

Returns

{Integer} A suitable zoom level for the specified resolution.  If no baselayer is set, returns null.

zoomTo

zoomTo: function(zoom)

Zoom to a specific zoom level

Parameters

zoom{Integer}

zoomIn

zoomIn: function()

Parameters

zoom{int}

zoomOut

zoomOut: function()

Parameters

zoom{int}

zoomToExtent

zoomToExtent: function(bounds)

Zoom to the passed in bounds, recenter

Parameters

bounds{OpenLayers.Bounds}

zoomToMaxExtent

zoomToMaxExtent: function()

Zoom to the full extent and recenter.

zoomToScale

zoomToScale: function(scale)

Zoom to a specified scale

Parameters

scale{float}

getLonLatFromViewPortPx

getLonLatFromViewPortPx: function (viewPortPx)

Parameters

viewPortPx{OpenLayers.Pixel}

Returns

{OpenLayers.LonLat} An OpenLayers.LonLat which is the passed-in view port OpenLayers.Pixel, translated into lon/lat by the current base layer.

getViewPortPxFromLonLat

getViewPortPxFromLonLat: function (lonlat)

Parameters

lonlat{OpenLayers.LonLat}

Returns

{OpenLayers.Pixel} An OpenLayers.Pixel which is the passed-in OpenLayers.LonLat, translated into view port pixels by the current base layer.

getLonLatFromPixel

getLonLatFromPixel: function (px)

Parameters

px{OpenLayers.Pixel}

Returns

{OpenLayers.LonLat} An OpenLayers.LonLat corresponding to the given OpenLayers.Pixel, translated into lon/lat by the current base layer

getPixelFromLonLat

getPixelFromLonLat: function (lonlat)

Parameters

lonlat{OpenLayers.LonLat}

Returns

{OpenLayers.Pixel} An OpenLayers.Pixel corresponding to the OpenLayers.LonLat translated into view port pixels by the current base layer.

getViewPortPxFromLayerPx

getViewPortPxFromLayerPx:function(layerPx)

Parameters

layerPx{OpenLayers.Pixel}

Returns

{OpenLayers.Pixel} Layer Pixel translated into ViewPort Pixel coordinates

getLayerPxFromViewPortPx

getLayerPxFromViewPortPx:function(viewPortPx)

Parameters

viewPortPx{OpenLayers.Pixel}

Returns

{OpenLayers.Pixel} ViewPort Pixel translated into Layer Pixel coordinates

getLonLatFromLayerPx

getLonLatFromLayerPx: function (px)

Parameters

px{OpenLayers.Pixel}

Returns

{OpenLayers.LonLat}

getLayerPxFromLonLat

getLayerPxFromLonLat: function (lonlat)

Parameters

lonlat{OpenLayers.LonLat} lonlat

Returns

{OpenLayers.Pixel} An OpenLayers.Pixel which is the passed-in OpenLayers.LonLat, translated into layer pixels by the current base layer

Constants

TILE_WIDTH

{Integer} 256 Default tile width (unless otherwise specified)

TILE_HEIGHT

{Integer} 256 Default tile height (unless otherwise specified)

Instances of this class represent a width/height pair
This class represents a longitude and latitude pair
Controls affect the display or behavior of the map.
A popup is a small div that can opened and closed on the map.
Instances of this class represent bounding boxes.
destroy:function()
Destroy this map
setOptions: function(options)
Change the map options
getTileSize: function()
Get the tile size for the map
getLayer: function(id)
Get a layer based on its id
setLayerZIndex: function (layer,
zIdx)
addLayer: function (layer)
addLayers: function (layers)
removeLayer: function(layer,
setNewBaseLayer)
Removes a layer from the map by removing its visual element (the layer.div property), then removing it from the map’s internal list of layers, setting the layer’s map property to null.
getNumLayers: function ()
{Int} The number of layers attached to the map.
getLayerIndex: function (layer)
setLayerIndex: function (layer,
idx)
Move the given layer to the specified (zero-based) index in the layer list, changing its z-index in the map display.
raiseLayer: function (layer,
delta)
Change the index of the given layer by delta.
setBaseLayer: function(newBaseLayer)
Allows user to specify one of the currently-loaded layers as the Map’s new base layer.
addControl: function (control,
px)
addControlToMap: function (control,
px)
getControl: function (id)
removeControl: function (control)
Remove a control from the map.
addPopup: function(popup,
exclusive)
removePopup: function(popup)
getSize: function ()
{OpenLayers.Size} An OpenLayers.Size object that represents the size, in pixels, of the div into which OpenLayers has been loaded.
updateSize: function()
This function should be called by any external code which dynamically changes the size of the map div (because mozilla wont let us catch the “onresize” for an element)
getCurrentSize: function()
{OpenLayers.Size} A new OpenLayers.Size object with the dimensions of the map div
calculateBounds: function(center,
resolution)
getCenter: function ()
{OpenLayers.LonLat}
getZoom: function ()
{Integer}
pan: function(dx,
dy)
Allows user to pan by a value of screen pixels
setCenter: function (lonlat,
zoom,
dragging,
forceZoomChange)
centerLayerContainer: function (lonlat)
This function takes care to recenter the layerContainerDiv.
isValidZoomLevel: function(zoomLevel)
isValidLonLat: function(lonlat)
getProjection: function()
{String} The Projection of the base layer.
getMaxResolution: function()
{String} The Map’s Maximum Resolution
getMaxExtent: function ()
{OpenLayers.Bounds}
getNumZoomLevels: function()
{Integer} The total number of zoom levels that can be displayed by the current baseLayer.
getExtent: function ()
{OpenLayers.Bounds} A Bounds object which represents the lon/lat bounds of the current viewPort.
getResolution: function ()
{Float} The current resolution of the map.
getScale: function ()
{Float} The current scale denominator of the map.
getZoomForExtent: function (bounds,
closest)
getZoomForResolution: function(resolution,
closest)
zoomTo: function(zoom)
Zoom to a specific zoom level
zoomIn: function()
zoomOut: function()
zoomToExtent: function(bounds)
Zoom to the passed in bounds, recenter
zoomToMaxExtent: function()
Zoom to the full extent and recenter.
zoomToScale: function(scale)
Zoom to a specified scale
getLonLatFromViewPortPx: function (viewPortPx)
getViewPortPxFromLonLat: function (lonlat)
getLonLatFromPixel: function (px)
getPixelFromLonLat: function (lonlat)
getViewPortPxFromLayerPx:function(layerPx)
getLayerPxFromViewPortPx:function(viewPortPx)
getLonLatFromLayerPx: function (px)
getLayerPxFromLonLat: function (lonlat)
Constructor for a new OpenLayers.Map instance.
This class represents a screen coordinate, in x and y coordinates