Skip to content

TileLayer

TileLayer is the base layer for displaying tile map services (Google Maps, OSM, AMap, etc.), extending Layer. It encapsulates tile URL template, subdomains, spatial reference, tile size, offset, world repeat, caching and load animation as options and a set of calculation/control methods.

js
import { Map, TileLayer } from "maptalks";

const map = new Map("map", { center: [0, 0], zoom: 2 });
const layer = new TileLayer("base", {
  urlTemplate: "https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png",
  subdomains: ["a", "b", "c", "d"],
}).addTo(map);

Constructor

js
new TileLayer(id, options)

Parameters:

  • id String layer id.
  • options Object layer options (optional).

Options

Tile-specific

OptionTypeDescriptionDefault
urlTemplateString|FunctionURL template, supports {x}{y}{z}{s}; or a function (x,y,z,domain)=>urlnull
subdomainsString[]subdomains replacing {s}null
spatialReferenceObjecttile layer spatial reference, defaults to map'snull
tileSizeNumber|[n,n]tile image size[256, 256]
offsetNumber[]|Functionoverall tile offset[0,0]
tileSystem[sx,sy,ox,oy]|Stringtile system or preset namenull
maxAvailableZoomNumbermax available tile zoom; reused beyond itnull
repeatWorldBooleanrepeat tiles beyond the worldtrue
backgroundBooleandraw background after interactiontrue
fadeAnimationBooleantile fade-in animationtrue
fadeDurationNumberfade-in duration (ms)167
crossOriginStringtile image crossOriginnull
errorUrlStringreplacement image on load errornull
tokenStringtoken replacing {token}null
customTagsObjectcustom tag values in templatenull
maxCacheSizeNumbermax cached tiles256
zoomOffsetNumberoffset between map and tile zoom0
cascadeTilesBooleandraw cascading tiles across zoomstrue
rendererStringrenderer type (gl/canvas)webgl?'gl':'canvas'
debugBooleandraw tile borders and coordinatesfalse

Inherited from Layer

attribution, minZoom, maxZoom, visible, opacity, zIndex, hitDetect, etc. — see Layer.

Member Methods

Tile URL & size

  • getTileUrl(x, y, z): string — get the URL of tile (x,y,z)
  • getTileSize(id?): Size — get the tile size

Tile calculation

  • getTiles(z, parentLayer): TilesType — get the tile descriptions at a zoom level
  • getTileId(x, y, zoom, id): string — get the unique tile id

Zoom & spatial reference

  • getSpatialReference(): SpatialReference — get the tile spatial reference
  • getMinZoom(): number / getMaxZoom(): number — get min/max zoom
  • getMaxAvailableZoom(): number — get the max available zoom

Layer control

  • forceReload(): this — force reload the layer
  • clear(): this — clear the layer
  • toJSON(): Object — export layer JSON

Static Methods

  • TileLayer.fromJSON(layerJSON): TileLayer | null — restore a layer from JSON

Events

EventFired when
tileloadtile loaded
tileerrortile load error
tiledeletetile deleted
forcereloadstart / forcereloadendforce reload
clearlayer cleared

Common layer events (idchange, setopacity, show/hide, layerload, add/remove, etc.) — see Layer.

TileSystem

TileSystem is a tile-system utility class describing the tile X/Y axis direction and the world projection origin. Pass a preset name or [sx, sy, ox, oy] to options.tileSystem.

Built-in presets:

ConstantMeaning
web-mercatorWeb Mercator (google/bing/AMap)
tms-global-mercatorTMS / mapbox mbtiles (EPSG:3857)
global-geodeticEPSG:4326 global geodetic grid
tms-global-geodeticTMS / OSGEO (EPSG:4326)
baiduBaidu Maps
js
layer.on("tileload", (e) => {
  console.log("tile loaded", e.tileInfo);
});