GridLayer
Generic class for handling a tiled grid of HTML elements. This is the base class for all tile layers and replaces TileLayer.Canvas
.
GridLayer can be extended to create a tiled grid of HTML elements like <canvas>
, <img>
or <div>
. GridLayer will handle creating and animating these DOM elements for you.
Usage example
Synchronous usage
To create a custom layer, extend GridLayer and implement the createTile()
method, which will be passed a Point
object with the x
, y
, and z
(zoom level) coordinates to draw your tile.
var CanvasLayer = L.GridLayer.extend({
createTile: function(coords){
// create a <canvas> element for drawing
var tile = L.DomUtil.create('canvas', 'leaflet-tile');
// setup tile width and height according to the options
var size = this.getTileSize();
tile.width = size.x;
tile.height = size.y;
// get a canvas context and draw something on it using coords.x, coords.y and coords.z
var ctx = tile.getContext('2d');
// return the tile so it can be rendered on screen
return tile;
}
});
Asynchronous usage
Tile creation can also be asynchronous, this is useful when using a third-party drawing library. Once the tile is finished drawing it can be passed to the done()
callback.
var CanvasLayer = L.GridLayer.extend({
createTile: function(coords, done){
var error;
// create a <canvas> element for drawing
var tile = L.DomUtil.create('canvas', 'leaflet-tile');
// setup tile width and height according to the options
var size = this.getTileSize();
tile.width = size.x;
tile.height = size.y;
// draw something asynchronously and pass the tile to the done() callback
setTimeout(function() {
done(error, tile);
}, 1000);
return tile;
}
});
Creation
Factory | Description |
---|---|
L.gridLayer( |
Creates a new instance of GridLayer with the supplied options. |
Options
Option | Type | Default | Description |
---|---|---|---|
tileSize |
Number|Point
| 256 |
Width and height of tiles in the grid. Use a number if width and height are equal, or L.point(width, height) otherwise. |
opacity |
Number
| 1.0 |
Opacity of the tiles. Can be used in the createTile() function. |
updateWhenIdle |
Boolean
| depends |
If false , new tiles are loaded during panning, otherwise only after it (for better performance). true by default on mobile browsers, otherwise false . |
updateWhenZooming |
Boolean
| true |
By default, a smooth zoom animation (during a touch zoom or a flyTo() ) will update grid layers every integer zoom level. Setting this option to false will update the grid layer only when the smooth animation ends. |
updateInterval |
Number
| 200 |
Tiles will not update more than once every updateInterval milliseconds when panning. |
attribution |
String
| null |
String to be shown in the attribution control, describes the layer data, e.g. "© OpenStreetMap contributors". |
zIndex |
Number
| 1 |
The explicit zIndex of the tile layer. |
bounds |
LatLngBounds
| undefined |
If set, tiles will only be loaded inside the set LatLngBounds . |
minZoom |
Number
| 0 |
The minimum zoom level that tiles will be loaded at. By default the entire map. |
maxZoom |
Number
| undefined |
The maximum zoom level that tiles will be loaded at. |
noWrap |
Boolean
| false |
Whether the layer is wrapped around the antimeridian. If true , the
GridLayer will only be displayed once at low zoom levels. Has no
effect when the map CRS doesn't wrap around. |
pane |
String
| 'tilePane' |
Map pane where the grid layer will be added. |
className |
String
| '' |
A custom class name to assign to the tile layer. Empty by default. |
keepBuffer |
Number
| 2 |
When panning the map, keep this many rows and columns of tiles before unloading them. |
Events
Event | Data | Description |
---|---|---|
loading
| ||
tileunload
| ||
tileloadstart
| ||
tileerror
| ||
tileload
| ||
load
|
Event | Data | Description |
---|---|---|
popupopen
| ||
popupclose
|
Event | Data | Description |
---|---|---|
tooltipopen
| ||
tooltipclose
|
Methods
Method | Returns | Description |
---|---|---|
bringToFront() |
this |
Brings the tile layer to the top of all tile layers. |
bringToBack() |
this |
Brings the tile layer to the bottom of all tile layers. |
getAttribution() |
String |
Used by the |
getContainer() |
HTMLElement |
Returns the HTML element that contains the tiles for this layer. |
setOpacity( |
this |
Changes the opacity of the grid layer. |
setZIndex( |
this |
Changes the zIndex of the grid layer. |
isLoading() |
Boolean |
Returns |
redraw() |
this |
Causes the layer to clear all the tiles and request them again. |
getTileSize() |
Point |
Normalizes the tileSize option into a point. Used by the |
Extension methods
Method | Returns | Description |
---|---|---|
createTile( |
HTMLElement |
Called only internally, must be overriden by classes extending |
Method | Returns | Description |
---|---|---|
bindPopup( |
this |
Binds a popup to the layer with the passed |
unbindPopup() |
this |
Removes the popup previously bound with |
openPopup( |
this |
Opens the bound popup at the specificed |
closePopup() |
this |
Closes the popup bound to this layer if it is open. |
togglePopup() |
this |
Opens or closes the popup bound to this layer depending on its current state. |
isPopupOpen() |
boolean |
Returns |
setPopupContent( |
this |
Sets the content of the popup bound to this layer. |
getPopup() |
Popup |
Returns the popup bound to this layer. |
Method | Returns | Description |
---|---|---|
bindTooltip( |
this |
Binds a tooltip to the layer with the passed |
unbindTooltip() |
this |
Removes the tooltip previously bound with |
openTooltip( |
this |
Opens the bound tooltip at the specificed |
closeTooltip() |
this |
Closes the tooltip bound to this layer if it is open. |
toggleTooltip() |
this |
Opens or closes the tooltip bound to this layer depending on its current state. |
isTooltipOpen() |
boolean |
Returns |
setTooltipContent( |
this |
Sets the content of the tooltip bound to this layer. |
getTooltip() |
Tooltip |
Returns the tooltip bound to this layer. |
Method | Returns | Description |
---|---|---|
addTo( |
this |
Adds the layer to the given map |
remove() |
this |
Removes the layer from the map it is currently active on. |
removeFrom( |
this |
Removes the layer from the given map |
getPane( |
HTMLElement |
Returns the |
Method | Returns | Description |
---|---|---|
on( |
this |
Adds a listener function ( |
on( |
this |
Adds a set of type/listener pairs, e.g. |
off( |
this |
Removes a previously added listener function. If no function is specified, it will remove all the listeners of that particular event from the object. Note that if you passed a custom context to |
off( |
this |
Removes a set of type/listener pairs. |
off() |
this |
Removes all listeners to all events on the object. |
fire( |
this |
Fires an event of the specified type. You can optionally provide an data object — the first argument of the listener function will contain its properties. The event might can optionally be propagated to event parents. |
listens( |
Boolean |
Returns |
once(…) |
this |
Behaves as |
addEventParent( |
this |
Adds an event parent - an |
removeEventParent( |
this |
Removes an event parent, so it will stop receiving propagated events |
addEventListener(…) |
this |
Alias to |
removeEventListener(…) |
this |
Alias to |
clearAllEventListeners(…) |
this |
Alias to |
addOneTimeEventListener(…) |
this |
Alias to |
fireEvent(…) |
this |
Alias to |
hasEventListeners(…) |
Boolean |
Alias to |
GridLayer
shall reimplement the following method.