# OpenLayers.Layer.Grid

Base class for layers that use a lattice of tiles. Create a new grid layer with the OpenLayers.Layer.Grid constructor.

#### Inherits from

- OpenLayers.Layer.HTTPRequest

Summary

OpenLayers.Layer.Grid | Base class for layers that use a lattice of tiles.
Properties |
tileSize | {OpenLayers.Size}
tileOriginCorner | {String} If the tileOrigin property is not provided, the tile origin will be derived from the layer’s maxExtent.
tileOrigin | {OpenLayers.LonLat} Optional origin for aligning the grid of tiles.
tileOptions | {Object} optional configuration options for OpenLayers.Tile instances created by this Layer, if supported by the tile class.
tileClass | {OpenLayers.Tile} The tile class to use for this layer.
grid | {Array(Array(OpenLayers.Tile))} This is an array of rows, each row is an array of tiles.
singleTile | {Boolean} Moves the layer into single-tile mode, meaning that one tile will be loaded.
ratio | {Float} Used only when in single-tile mode, this specifies the ratio of the size of the single tile to the size of the map.
buffer | {Integer} Used only when in gridded mode, this specifies the number of extra rows and colums of tiles on each side which will surround the minimum grid tiles to cover the map.
transitionEffect | {String} The transition effect to use when the map is zoomed.
numLoadingTiles | {Integer} How many tiles are still loading?
serverResolutions | {Array(Number}} This property is documented in subclasses as an API property.
loading | {Boolean} Indicates if tiles are being loaded.
backBuffer | {DOMElement} The back buffer.
gridResolution | {Number} The resolution of the current grid.
backBufferResolution | {Number} The resolution of the current back buffer.
backBufferLonLat | {Object} The top-left corner of the current back buffer.
backBufferTimerId | {Number} The id of the back buffer timer.
removeBackBufferDelay | {Number} Delay for removing the backbuffer when all tiles have finished loading.
className | {String} Name of the class added to the layer div.
gridLayout | {Object} Object containing properties tilelon, tilelat, startcol, startrow
rowSign | {Number} 1 for grids starting at the top, -1 for grids starting at the bottom.
transitionendEvents | {Array} Event names for transitionend
Constructor |
OpenLayers.Layer.Grid | Create a new grid layer
Functions |
initProperties | Set any properties that depend on the value of singleTile.
setMap |
removeMap | Called when the layer is removed from the map.
destroy | Deconstruct the layer and clear the grid.
mergeNewParams | Refetches tiles with new params merged, keeping a backbuffer.
clearGrid | Go through and remove all tiles from the grid, calling destroy() on each of them to kill circular references
addOptions |
clone | Create a clone of this layer
moveTo | This function is called whenever the map is moved.
getTileData | Given a map location, retrieve a tile and the pixel offset within that tile corresponding to the location.
destroyTile |
getServerResolution | Return the closest server-supported resolution.
getServerZoom | Return the zoom value corresponding to the best matching server resolution, taking into account serverResolutions and <zoomOffset>.
applyBackBuffer | Create, insert, scale and position a back buffer for the layer.
createBackBuffer | Create a back buffer.
removeBackBuffer | Remove back buffer from DOM.
moveByPx | Move the layer based on pixel vector.
setTileSize | Check if we are in singleTile mode and if so, set the size as a ratio of the map size (as specified by the layer’s ‘ratio’ property).
getTilesBounds | Return the bounds of the tile grid.
initSingleTile |
calculateGridLayout | Generate parameters for the grid layout.
getTileOrigin | Determine the origin for aligning the grid of tiles.
getTileBoundsForGridIndex |
initGriddedTiles |
getMaxExtent | Get this layer’s maximum extent.
addTile | Create a tile, initialize it, and add it to the layer div.
addTileMonitoringHooks | This function takes a tile as input and adds the appropriate hooks to the tile so that the layer can keep track of the loading tiles.
removeTileMonitoringHooks | This function takes a tile as input and removes the tile hooks that were added in addTileMonitoringHooks()
moveGriddedTiles |
shiftRow | Shifty grid work
shiftColumn | Shift grid work in the other dimension
removeExcessTiles | When the size of the map or the buffer changes, we may need to remove some excess rows and columns.
onMapResize | For singleTile layers, this will set a new tile size according to the dimensions of the map pane.
getTileBounds | Returns The tile bounds for a layer given a pixel location.

### Properties

### tileSize

{OpenLayers.Size}

### tileOriginCorner

{String} If the tileOrigin property is not provided, the tile origin will be derived from the layer’s maxExtent. The corner of the maxExtent used is determined by this property. Acceptable values are “tl” (top left), “tr” (top right), “bl” (bottom left), and “br” (bottom right). Default is “bl”.

### tileOrigin

{OpenLayers.LonLat} Optional origin for aligning the grid of tiles. If provided, requests for tiles at all resolutions will be aligned with this location (no tiles shall overlap this location). If not provided, the grid of tiles will be aligned with the layer’s maxExtent. Default is ``null``.

### tileOptions

{Object} optional configuration options for OpenLayers.Tile instances created by this Layer, if supported by the tile class.

### tileClass

{OpenLayers.Tile} The tile class to use for this layer. Defaults is OpenLayers.Tile.Image.

### grid

{Array(Array(OpenLayers.Tile))} This is an array of rows, each row is an array of tiles.

### singleTile

{Boolean} Moves the layer into single-tile mode, meaning that one tile will be loaded. The tile’s size will be determined by the ‘ratio’ property. When the tile is dragged such that it does not cover the entire viewport, it is reloaded.

### ratio

{Float} Used only when in single-tile mode, this specifies the ratio of the size of the single tile to the size of the map. Default value is 1.5.

### buffer

{Integer} Used only when in gridded mode, this specifies the number of extra rows and colums of tiles on each side which will surround the minimum grid tiles to cover the map. For very slow loading layers, a larger value may increase performance somewhat when dragging, but will increase bandwidth use significantly.

### transitionEffect

{String} The transition effect to use when the map is zoomed. Two posible values:

”resize” | Existing tiles are resized on zoom to provide a visual effect of the zoom having taken place immediately. As the new tiles become available, they are drawn on top of the resized tiles (this is the default setting).
”map-resize” | Existing tiles are resized on zoom and placed below the base layer. New tiles for the base layer will cover existing tiles. This setting is recommended when having an overlay duplicated during the transition is undesirable (e.g. street labels or big transparent fills).
null | No transition effect.

Using “resize” on non-opaque layers can cause undesired visual effects. Set transitionEffect to null in this case.

### numLoadingTiles

{Integer} How many tiles are still loading?

### serverResolutions

{Array(Number}} This property is documented in subclasses as an API property.

### loading

{Boolean} Indicates if tiles are being loaded.

### backBuffer

{DOMElement} The back buffer.

### gridResolution

{Number} The resolution of the current grid. Used for backbuffer and client zoom. This property is updated every time the grid is initialized.

### backBufferResolution

{Number} The resolution of the current back buffer. This property is updated each time a back buffer is created.

### backBufferLonLat

{Object} The top-left corner of the current back buffer. Includes lon and lat properties. This object is updated each time a back buffer is created.

### backBufferTimerId

{Number} The id of the back buffer timer. This timer is used to delay the removal of the back buffer, thereby preventing flash effects caused by tile animation.

### removeBackBufferDelay

{Number} Delay for removing the backbuffer when all tiles have finished loading. Can be set to 0 when no css opacity transitions for the olTileImage class are used. Default is 0 for singleTile layers, 2500 for tiled layers. See className for more information on tile animation.

### className

{String} Name of the class added to the layer div. If not set in the options passed to the constructor then className defaults to “olLayerGridSingleTile” for single tile layers (see singleTile), and “olLayerGrid” for non single tile layers.

#### Note

The displaying of tiles is not animated by default for single tile layers - OpenLayers’ default theme (style.css) includes this:

>     .olLayerGrid .olTileImage {
>         -webkit-transition: opacity 0.2s linear;
>         -moz-transition: opacity 0.2s linear;
>         -o-transition: opacity 0.2s linear;
>         transition: opacity 0.2s linear;
>      }

To animate tile displaying for any grid layer the following CSS rule can be used:

>     .olTileImage {
>         -webkit-transition: opacity 0.2s linear;
>         -moz-transition: opacity 0.2s linear;
>         -o-transition: opacity 0.2s linear;
>         transition: opacity 0.2s linear;
>     }

In that case, to avoid flash effects, removeBackBufferDelay should not be zero.

### gridLayout

{Object} Object containing properties tilelon, tilelat, startcol, startrow

### rowSign

{Number} 1 for grids starting at the top, -1 for grids starting at the bottom. This is used for several grid index and offset calculations.

### transitionendEvents

{Array} Event names for transitionend

### Constructor

### OpenLayers.Layer.Grid

Create a new grid layer

#### Parameters

name | {String}
url | {String}
params | {Object}
options | {Object} Hashtable of extra options to tag onto the layer

### Functions

### initProperties

> initProperties: function()

Set any properties that depend on the value of singleTile. Currently sets removeBackBufferDelay and className

### setMap

> setMap: function( | map | )

#### Parameters

map | {OpenLayers.Map} The map.

### removeMap

> removeMap: function( | map | )

Called when the layer is removed from the map.

#### Parameters

map | {OpenLayers.Map} The map.

### destroy

> destroy: function()

Deconstruct the layer and clear the grid.

### mergeNewParams

Refetches tiles with new params merged, keeping a backbuffer. Each loading new tile will have a css class of ‘.olTileReplacing’. If a stylesheet applies a ‘display: none’ style to that class, any fade-in transition will not apply, and backbuffers for each tile will be removed as soon as the tile is loaded.

#### Parameters

newParams | {Object}

#### Returns

redrawn: {Boolean} whether the layer was actually redrawn.

### clearGrid

> clearGrid:function()

Go through and remove all tiles from the grid, calling destroy() on each of them to kill circular references

### addOptions

> addOptions: function ( | newOptions,
> | reinitialize | )

#### Parameters

newOptions | {Object}
reinitialize | {Boolean} If set to true, and if resolution options of the current baseLayer were changed, the map will be recentered to make sure that it is displayed with a valid resolution, and a changebaselayer event will be triggered.

### clone

> clone: function ( | obj | )

Create a clone of this layer

#### Parameters

obj | {Object} Is this ever used?

#### Returns

{OpenLayers.Layer.Grid} An exact clone of this OpenLayers.Layer.Grid

### moveTo

> moveTo:function( | bounds,
> | zoomChanged,
> | dragging | )

This function is called whenever the map is moved. All the moving of actual ‘tiles’ is done by the map, but moveTo’s role is to accept a bounds and make sure the data that that bounds requires is pre-loaded.

#### Parameters

bounds | {OpenLayers.Bounds}
zoomChanged | {Boolean}
dragging | {Boolean}

### getTileData

> getTileData: function( | loc | )

Given a map location, retrieve a tile and the pixel offset within that tile corresponding to the location. If there is not an existing tile in the grid that covers the given location, null will be returned.

#### Parameters

loc | {OpenLayers.LonLat} map location

#### Returns

{Object} Object with the following properties: tile ({OpenLayers.Tile}), i ({Number} x-pixel offset from top left), and j ({Integer} y-pixel offset from top left).

### destroyTile

> destroyTile: function( | tile | )

#### Parameters

tile | {OpenLayers.Tile}

### getServerResolution

> getServerResolution: function( | resolution | )

Return the closest server-supported resolution.

#### Parameters

resolution | {Number} The base resolution. If undefined the map resolution is used.

#### Returns

{Number} The closest server resolution value.

### getServerZoom

> getServerZoom: function()

Return the zoom value corresponding to the best matching server resolution, taking into account serverResolutions and <zoomOffset>.

#### Returns

{Number} The closest server supported zoom. This is not the map zoom level, but an index of the server’s resolutions array.

### applyBackBuffer

> applyBackBuffer: function( | resolution | )

Create, insert, scale and position a back buffer for the layer.

#### Parameters

resolution | {Number} The resolution to transition to.

### createBackBuffer

> createBackBuffer: function()

Create a back buffer.

#### Returns

{DOMElement} The DOM element for the back buffer, undefined if the grid isn’t initialized yet.

### removeBackBuffer

> removeBackBuffer: function()

Remove back buffer from DOM.

### moveByPx

> moveByPx: function( | dx,
> | dy | )

Move the layer based on pixel vector.

#### Parameters

dx | {Number}
dy | {Number}

### setTileSize

> setTileSize: function( | size | )

Check if we are in singleTile mode and if so, set the size as a ratio of the map size (as specified by the layer’s ‘ratio’ property).

#### Parameters

size | {OpenLayers.Size}

### getTilesBounds

> getTilesBounds: function()

Return the bounds of the tile grid.

#### Returns

{OpenLayers.Bounds} A Bounds object representing the bounds of all the currently loaded tiles (including those partially or not at all seen onscreen).

### initSingleTile

> initSingleTile: function( | bounds | )

#### Parameters

bounds | {OpenLayers.Bounds}

### calculateGridLayout

> calculateGridLayout: function( | bounds,
> | origin,
> | resolution | )

Generate parameters for the grid layout.

#### Parameters

bounds | {<OpenLayers.Bound>|Object} OpenLayers.Bounds or an object with a ‘left’ and ‘top’ properties.
origin | {<OpenLayers.LonLat>|Object} OpenLayers.LonLat or an object with a ‘lon’ and ‘lat’ properties.
resolution | {Number}

#### Returns

{Object} Object containing properties tilelon, tilelat, startcol, startrow

### getTileOrigin

> getTileOrigin: function()

Determine the origin for aligning the grid of tiles. If a tileOrigin property is supplied, that will be returned. Otherwise, the origin will be derived from the layer’s maxExtent property. In this case, the tile origin will be the corner of the maxExtent given by the tileOriginCorner property.

#### Returns

{OpenLayers.LonLat} The tile origin.

### getTileBoundsForGridIndex

> getTileBoundsForGridIndex: function( | row,
> | col | )

#### Parameters

row | {Number} The row of the grid
col | {Number} The column of the grid

#### Returns

{OpenLayers.Bounds} The bounds for the tile at (row, col)

### initGriddedTiles

> initGriddedTiles:function( | bounds | )

#### Parameters

bounds | {OpenLayers.Bounds}

### getMaxExtent

> getMaxExtent: function()

Get this layer’s maximum extent. (Implemented as a getter for potential specific implementations in sub-classes.)

#### Returns

{OpenLayers.Bounds}

### addTile

> addTile: function( | bounds,
> | position | )

Create a tile, initialize it, and add it to the layer div.

Parameters bounds - {OpenLayers.Bounds} position - {OpenLayers.Pixel}

#### Returns

{OpenLayers.Tile} The added OpenLayers.Tile

### addTileMonitoringHooks

> addTileMonitoringHooks: function( | tile | )

This function takes a tile as input and adds the appropriate hooks to the tile so that the layer can keep track of the loading tiles.

#### Parameters

tile | {OpenLayers.Tile}

### removeTileMonitoringHooks

> removeTileMonitoringHooks: function( | tile | )

This function takes a tile as input and removes the tile hooks that were added in addTileMonitoringHooks()

#### Parameters

tile | {OpenLayers.Tile}

### moveGriddedTiles

> moveGriddedTiles: function()

### shiftRow

> shiftRow: function( | prepend,
> | tileSize | )

Shifty grid work

#### Parameters

prepend | {Boolean} if true, prepend to beginning. if false, then append to end
tileSize | {Object} rendered tile size; object with w and h properties

### shiftColumn

> shiftColumn: function( | prepend,
> | tileSize | )

Shift grid work in the other dimension

#### Parameters

prepend | {Boolean} if true, prepend to beginning. if false, then append to end
tileSize | {Object} rendered tile size; object with w and h properties

### removeExcessTiles

> removeExcessTiles: function( | rows,
> | columns | )

When the size of the map or the buffer changes, we may need to remove some excess rows and columns.

#### Parameters

rows | {Integer} Maximum number of rows we want our grid to have.
columns | {Integer} Maximum number of columns we want our grid to have.

### onMapResize

> onMapResize: function()

For singleTile layers, this will set a new tile size according to the dimensions of the map pane.

### getTileBounds

> getTileBounds: function( | viewPortPx | )

Returns The tile bounds for a layer given a pixel location.

#### Parameters

viewPortPx | {OpenLayers.Pixel} The location in the viewport.

#### Returns

{OpenLayers.Bounds} Bounds of the tile at the given pixel location.

Instances of this class represent a width/height pair
{OpenLayers.LonLat} Optional origin for aligning the grid of tiles.
{<OpenLayers.Bounds>|Array} If provided as an array, the array should consist of four values (left, bottom, right, top).
This class represents a longitude and latitude pair

> initProperties: function()

Set any properties that depend on the value of singleTile.

> setMap: function( | map | )

> removeMap: function( | map | )

Called when the layer is removed from the map.

> destroy: function()

Deconstruct the layer and clear the grid.

> clearGrid:function()

Go through and remove all tiles from the grid, calling destroy() on each of them to kill circular references

> addOptions: function ( | newOptions,
> | reinitialize | )

> clone: function ( | obj | )

Create a clone of this layer

> moveTo:function( | bounds,
> | zoomChanged,
> | dragging | )

This function is called whenever the map is moved.

> getTileData: function( | loc | )

Given a map location, retrieve a tile and the pixel offset within that tile corresponding to the location.

> destroyTile: function( | tile | )

> getServerResolution: function( | resolution | )

Return the closest server-supported resolution.

> getServerZoom: function()

Return the zoom value corresponding to the best matching server resolution, taking into account serverResolutions and zoomOffset.
{Array(Number}} This property is documented in subclasses as an API property.

> applyBackBuffer: function( | resolution | )

Create, insert, scale and position a back buffer for the layer.

> createBackBuffer: function()

Create a back buffer.

> removeBackBuffer: function()

Remove back buffer from DOM.

> moveByPx: function( | dx,
> | dy | )

Move the layer based on pixel vector.

> setTileSize: function( | size | )

Check if we are in singleTile mode and if so, set the size as a ratio of the map size (as specified by the layer’s ‘ratio’ property).

> getTilesBounds: function()

Return the bounds of the tile grid.

> initSingleTile: function( | bounds | )

> calculateGridLayout: function( | bounds,
> | origin,
> | resolution | )

Generate parameters for the grid layout.

> getTileOrigin: function()

Determine the origin for aligning the grid of tiles.

> getTileBoundsForGridIndex: function( | row,
> | col | )

> initGriddedTiles:function( | bounds | )

> getMaxExtent: function()

Get this layer’s maximum extent.

> addTile: function( | bounds,
> | position | )

Create a tile, initialize it, and add it to the layer div.

> addTileMonitoringHooks: function( | tile | )

This function takes a tile as input and adds the appropriate hooks to the tile so that the layer can keep track of the loading tiles.

> removeTileMonitoringHooks: function( | tile | )

This function takes a tile as input and removes the tile hooks that were added in addTileMonitoringHooks()

> moveGriddedTiles: function()

> shiftRow: function( | prepend,
> | tileSize | )

Shifty grid work

> shiftColumn: function( | prepend,
> | tileSize | )

Shift grid work in the other dimension

> removeExcessTiles: function( | rows,
> | columns | )

When the size of the map or the buffer changes, we may need to remove some excess rows and columns.

> onMapResize: function()

For singleTile layers, this will set a new tile size according to the dimensions of the map pane.

> getTileBounds: function( | viewPortPx | )

Returns The tile bounds for a layer given a pixel location.
Create a new grid layer
{Boolean} Moves the layer into single-tile mode, meaning that one tile will be loaded.
{String} Name of the class added to the layer div.
{Number} Delay for removing the backbuffer when all tiles have finished loading.
Instances of OpenLayers.Map are interactive maps embedded in a web page.
Instances of this class represent bounding boxes.
{String} If the tileOrigin property is not provided, the tile origin will be derived from the layer’s maxExtent.
This class represents a screen coordinate, in x and y coordinates
Close