Map
Map is the core class of maptalks. It creates and manages a map on a given HTML container, handling the spatial reference (projection), view state (center/zoom/pitch/bearing), layer management, interaction (drag/zoom/rotate/pitch), coordinate conversion, and the event system.
The inheritance chain is Map → Handlerable(Eventable(Renderable(Class))), so a Map has event (on/off/once/fire), configuration (config), rendering (getRendererClass) and interaction-handler capabilities.
js
import { Map, TileLayer } from "maptalks";
const map = new Map("map", {
center: [121.47, 31.23],
zoom: 14,
baseLayer: new TileLayer("base", {
urlTemplate: "https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png",
subdomains: ["a", "b", "c", "d"],
}),
});Constructor
js
new Map(container, options)Parameters:
- container
String | HTMLElementThe map container — a DOM element or its id. - options
Objectrequired. Common construction options:
| Option | Type | Description |
|---|---|---|
center | Number[] | Coordinate | required initial map center |
zoom | Number | required initial zoom level |
spatialReference | Object | spatial reference, default EPSG:3857 |
baseLayer | Layer | initial base layer |
layers | Layer[] | other initial layers |
pitch | Number | initial pitch (degrees) |
bearing | Number | initial bearing (degrees) |
Options
View & zoom
| Option | Type | Description | Default |
|---|---|---|---|
centerCross | Boolean | Show a red cross at map center | false |
seamlessZoom | Boolean | Use seamless zoom mode | true |
zoomAnimation | Boolean | Enable zoom animation | true |
zoomAnimationDuration | Number | Zoom animation duration (ms) | 330 |
panAnimation | Boolean | Continue pan animation after drag/touch | true |
panAnimationDuration | Number | Pan animation duration (ms) | 600 |
rotateAnimation | Boolean | Continue rotation animation | true |
rotateAnimationDuration | Number | Rotation animation duration (ms) | 800 |
maxZoom | Number | Maximum zoom level | null |
minZoom | Number | Minimum zoom level | null |
maxExtent | Extent | Maximum extent restriction | null |
maxPitch | Number | Maximum pitch | 80 |
maxVisualPitch | Number | Maximum visual pitch | 70 |
Interaction
| Option | Type | Description | Default |
|---|---|---|---|
draggable | Boolean | Enable drag | true |
dragPan | Boolean | Drag to pan | true |
dragRotate | Boolean | Right-click or Ctrl+left-click drag to rotate | true |
dragPitch | Boolean | Right-click or Ctrl+left-click drag to pitch | true |
switchDragButton | Boolean | Switch left button to rotate, right to move | false |
zoomable | Boolean | Enable zoom | true |
scrollWheelZoom | Boolean | Scroll-wheel zoom | true |
doubleClickZoom | Boolean | Double-click zoom | true |
touchGesture | Boolean | Allow two-finger touch zoom/rotate/pitch | true |
touchZoom | Boolean | Touch zoom | true |
touchRotate | Boolean | Touch rotate | true |
touchPitch | Boolean | Touch pitch | true |
boxZoom | Boolean | Box zoom | false |
Rendering & performance
| Option | Type | Description | Default |
|---|---|---|---|
renderer | String | String[] | Renderer type (canvas / gl / gpu, see the note below) | ['canvas','gl','gpu'] |
devicePixelRatio | Number | Override device DPR | null |
hitDetect | Boolean | Layer hit detection for cursor style | true |
hitDetectLimit | Number | Max layers to hit-detect | 5 |
fpsOnInteracting | Number | FPS while interacting, 0 to disable | 25 |
stopRenderOnOffscreen | Boolean | Stop rendering when container is offscreen | true |
fog | Boolean | Draw fog in the distance | true |
fogColor | Number[] | Fog color [r,g,b] | [233,233,233] |
cameraFarUndergroundInMeter | Number | Camera far distance underground (m) | 2000 |
The map uses the first available renderer in the renderer list, in order. The default ['canvas','gl','gpu'] prefers 2D canvas rendering; pass 'gpu' explicitly to force WebGPU rendering (MapGPURenderer).
'canvas'— 2D canvas rendering, the default path for 2D layers (tiles, markers, etc.).'gl'— WebGL rendering (MapGLRenderer), the path for 3D scenes.'gpu'— WebGPU rendering (MapGPURenderer), registered viaMap.registerRenderer('gpu', MapGPURenderer); it creates the context withcanvas.getContext('webgpu')and usesreshader.GraphicsDeviceas the GPU device (isWebGPU()returnstrue).
WebGPU rendering notes (see WebGPU rendering):
- Browser/device requirement: requires a WebGPU-capable browser (modern Chromium such as desktop Chrome/Edge, and newer Safari and Firefox) and a WebGPU-capable GPU/driver; check
navigator.gputo detect support. - Screenshot & export: WebGPU does not preserve the drawing buffer by default, so
toDataURL()/screenshot relies on thepreserveGpuDrawingBufferoption; when enabled, each frame is read back into a readback canvas (device.preserveDrawingBuffer).
Controls
| Option | Type | Description | Default |
|---|---|---|---|
control | Boolean | Allow adding controls | true |
attribution | Boolean | Object | Show attribution control | true |
zoomControl | Boolean | Object | Zoom control | false |
scaleControl | Boolean | Object | Scale control | false |
overviewControl | Boolean | Object | Overview control | false |
Member Methods
Lifecycle & basics
isLoaded(): boolean— whether the map is loadedgetContainer(): HTMLElement— get the map containergetSize(): Size— get the map pixel sizeremove(): Map— remove the mapisRemoved(): boolean— whether the map is removedcheckSize(force?): Map— check and update container sizesetDevicePixelRatio(dpr): Map/getDevicePixelRatio(): number— set/get DPRtoJSON(options?): Object— export a map JSON snapshotgetRenderer()— get the renderer instance
View & camera
getZoom(): number/setZoom(zoom, options?): Map— get/set zoomzoomIn(): Map/zoomOut(): Map— zoom in/out one levelgetCenter(): Coordinate/setCenter(center, padding?): Map— get/set centersetCenterAndZoom(center, zoom?, padding?): Map— set center and zoom togethergetView(): MapViewType/setView(view): Map— get/set view (center/zoom/pitch/bearing)getPitch(): number/setPitch(pitch): Map— get/set pitchgetBearing(): number/setBearing(bearing): Map— get/set bearinggetFov(): number/setFov(fov): Map— get/set FOVgetResolution(zoom?): number— get resolutionsetCameraPosition(coordinate): Map— set camera from a coordinatesetCameraMovements(frameOptions, option?): Map— run sequential camera frames (auto flight)lookAt(params): Map— point the camera at a coordinateisZooming(): boolean— whether zoomingisAnimating(): boolean— whether animating
Pan & animation
panTo(coordinate, options?, step?): Map— smoothly pan to a targetpanBy(offset, options?, step?): Map— pan by pixel offsetanimateTo(view, options?, step?): Player— animate the viewflyTo(view, options?, step?): this— smoothly fly to a viewisRotating(): boolean/isMoving(): boolean— rotation/movement state
Layer management
getBaseLayer(): Layer/setBaseLayer(baseLayer): Map— get/set base layerremoveBaseLayer(): Map— remove the base layergetLayers(filter?): Layer[]— get layersgetLayer(id): Layer | null— get a layer by idaddLayer(layers, ...otherLayers): this— add layersremoveLayer(layers): this— remove layerssortLayers(layers): Map— sort layers
Spatial reference & extent
getSpatialReference()/setSpatialReference(ref): Map— get/set spatial referencegetProjection()— get projectiongetFullExtent(): Extent— get full extentgetMaxExtent(): Extent/setMaxExtent(extent): Map— get/set max extent
Coordinate conversion
coordinateToPoint(coordinate, zoom?, out?): Point— coordinate to 2D pointpointToCoordinate(point, zoom?, out?): Coordinate— 2D point to coordinatecoordinateToContainerPoint(coordinate, zoom?, out?): Point— coordinate to container pointcontainerPointToCoordinate(containerPoint, out?): Coordinate— container point to coordinategetExtent(): Extent— get the current view geographic extentgetContainerExtent(): PointExtent— get the container extent
Distance & measurement
distanceToPixel(xDist, yDist, zoom?): Size— geographic distance to pixelspixelToDistance(width, height): number— pixels to geographic distancecomputeLength(coord1, coord2): number— distance between two coordinates (m)computeGeometryLength(geometry): number— geometry length (m)computeGeometryArea(geometry): number— geometry area (sq m)
Query & identify
identify(opts, callback): Map— identify geometry at a coordinateidentifyAtPoint(point, opts, callback): Map— identify geometry at a container pointgetCollisionIndex(): CollisionIndex— get the collision index
View history
getViewHistory(): MapViewType[]— get view historyhasPreviousView(): boolean/hasNextView(): boolean— has previous/next viewzoomToPreviousView(options?): MapViewType/zoomToNextView(options?): MapViewType— go to previous/next view
Export & fullscreen
toDataURL(options?): string | null— export an imageisFullScreen(): boolean— whether fullscreenrequestFullScreen(dom?): Map— request fullscreencancelFullScreen(): Map— cancel fullscreen
Static Methods
Map.VERSION— version numberMap.fromJSON(container, profile, options?): Map— rebuild a map from JSONMap.addOnLoadHook(fn, ...args): Map— add a hook after map load
Events
View & interaction
| Event | Fired when |
|---|---|
movestart / moving / moveend | pan starts/ongoing/ends |
zoomstart / zooming / zoomend | zoom starts/ongoing/ends |
rotatestart / rotate / rotateend | bearing changes |
pitchstart / pitch / pitchend | pitch changes |
dragrotatestart / dragrotating / dragrotateend | drag-rotate |
fovchange | FOV changes |
resize | container resize |
viewchange | view changes |
Layers
| Event | Fired when |
|---|---|
addlayer | layer added |
removelayer | layer removed |
setbaselayer | base layer set |
baselayerchangestart / baselayerload / baselayerchangeend | base layer changes |
Animation
| Event | Fired when |
|---|---|
animatestart / animating / animateend | animation |
animateinterrupted | animation interrupted |
Lifecycle
| Event | Fired when |
|---|---|
removestart / removeend | map removed |
fullscreenstart / fullscreenend / cancelfullscreen | fullscreen |
js
map.on("zoomend", (e) => {
console.log("zoom:", map.getZoom(), "center:", map.getCenter());
});