GridLayer
Quick answer
L.GridLayer is the abstract tiled layer base that TileLayer and WMS build on. Subclass it and implement createTile when tile content must be generated, such as canvas drawings, custom text or procedural grids. It also manages loading state, buffering and redraw events.
L.gridLayer( options ) → GridLayer
Key points
- createTile must call done with an error and tile for async content so load events fire.
- maxNativeZoom stretches the highest available tiles instead of requesting missing levels.
- keepBuffer controls how many off-screen tile rings stay cached in memory.
Example
var LabelGrid = L.GridLayer.extend({
createTile: function (coords) {
var tile = L.DomUtil.create('canvas', 'leaflet-tile');
var size = this.getTileSize();
tile.width = size.x;
tile.height = size.y;
var ctx = tile.getContext('2d');
ctx.fillStyle = '#f4f6f8';
ctx.fillRect(0, 0, size.x, size.y);
ctx.strokeStyle = '#d5dae1';
ctx.strokeRect(0.5, 0.5, size.x - 1, size.y - 1);
ctx.fillStyle = '#58606e';
ctx.font = '12px sans-serif';
ctx.fillText(coords.z + '/' + coords.x + '/' + coords.y, 10, 20);
return tile;
}
});
var labels = new LabelGrid({ tileSize: 256, opacity: 0.9 }).addTo(map);
labels.on('tileerror', function (e) { console.warn(e.coords, e.error); });
Notes
- Skipping the done callback on async tiles leaves isLoading permanently true.
- Leaving maxNativeZoom unset above the provider limit produces blank screens at high zoom.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). GridLayer. https://www.jingweizhi.com/en/leaflet/api/grid-layer.html (accessed 2026-11-03).