Map

The Map object represents the map on your page. It exposes methods and properties that enable you to programmatically change the map, and fires events as users interact with it.

You create a Map by specifying a container and other options. Then SDK JS initializes the map on the page and returns your Map object.

Extends Evented.

Example

Parameters

Options to provide to the Map constructor (MapOptions)

Properties

options.apiKey MapTiler logo Define the MapTiler Cloud API key to be used. This is equivalent to setting config.apiKey and will overwrite it.
options.container The HTML element in which SDK JS will render the map, or the element's string id . The specified element must have no children.
options.style MapTiler logo The map's style. This must be:
  • ReferenceMapStyle (e.g. MapStyle.STREETS)
  • MapStyleVariant (e.g. MapStyle.STREETS.DARK)
  • MapTIler Style ID (e.g. “streets-v2”)
  • uuid of custom style
  • an a JSON object conforming to the schema described in the GL Style Specification
  • a URL to such JSON.
options.language MapTiler logo Define the language of the map. This can be done directly with a language ISO code (eg. "en") or with a built-in Languages shorthand (eg. Language.ENGLISH) This applies only for the map instance, supersedes the config.primaryLanguage.
options.center
default: [0,0]
The initial geographical centerpoint of the map. If center is not specified in the constructor options, SDKJS will look for it in the map's style object. If it is not specified in the style, either, it will default to [0, 0] Note: SDK JS uses longitude, latitude coordinate order (as opposed to latitude, longitude) to match GeoJSON.
options.zoom
default: 0
The initial zoom level of the map. If zoom is not specified in the constructor options, SDK JS will look for it in the map's style object. If it is not specified in the style, either, it will default to 0 .
options.projection MapTiler logo
default: "mercator"
This will overwrite the projection property from the style (if any) and will persist it later if the map style was to change.
options.bearing
default: 0
The initial bearing (rotation) of the map, measured in degrees counter-clockwise from north. If bearing is not specified in the constructor options, SDK JS will look for it in the map's style object. If it is not specified in the style, either, it will default to 0 .
options.pitch
default: 0
The initial pitch (tilt) of the map, measured in degrees away from the plane of the screen (0-85). If pitch is not specified in the constructor options, SDK JS will look for it in the map's style object. If it is not specified in the style, either, it will default to 0 . Values greater than 60 degrees are experimental and may result in rendering issues. If you encounter any, please raise an issue with details in the MapLibre project.
options.attributionControl
default: true
If true , an AttributionControl will be added to the map.
options.bearingSnap
default: 7
The threshold, measured in degrees, that determines when the map's bearing will snap to north. For example, with a bearingSnap of 7, if the user rotates the map within 7 degrees of north, the map will automatically snap to exact north.
options.bounds The initial bounds of the map. If bounds is specified, it overrides center and zoom constructor options.
options.boxZoom
default: true
If true , the "box zoom" interaction is enabled (see BoxZoomHandler ).
options.cancelPendingTileRequestsWhileZooming
default: true
Determines whether to cancel, or retain, tiles from the current viewport which are still loading but which belong to a farther (smaller) zoom level than the current one. If true, when zooming in, tiles which didn't manage to load for previous zoom levels will become canceled. This might save some computing resources for slower devices, but the map details might appear more abruptly at the end of the zoom. If false, when zooming in, the previous zoom level(s) tiles will progressively appear, giving a smoother map details experience. However, more tiles will be rendered in a short period of time.
options.canvasContextAttributes
default: antialias: false, powerPreference: 'high-performance', preserveDrawingBuffer: false, failIfMajorPerformanceCaveat: false, desynchronized: false, contextType: 'webgl2withfallback'
Set of WebGLContextAttributes that are applied to the WebGL context of the map. See getContext for more details. contextType, can be set to webgl2 or webgl to force a WebGL version. Not setting it, MapTiler SDK will do it's best to get a suitable context.
options.centerClampedToGround
default: true
If true, the elevation of the center point will automatically be set to the terrain elevation (or zero if terrain is not enabled). If false, the elevation of the center point will default to sea level and will not automatically update.

Needs to be set to false to keep the camera above ground when pitch > 90 degrees.

options.clickTolerance
default: 3
The max number of pixels a user can shift the mouse pointer during a click for it to be considered a valid click (as opposed to a mouse drag).
options.collectResourceTiming
default: false
If true , Resource Timing API information will be collected for requests made by GeoJSON and Vector Tile web workers (this information is normally inaccessible from the main Javascript thread). Information will be returned in a resourceTiming property of relevant data events.
options.cooperativeGestures
(boolean | GestureOptions)?
default: false
If true or set to an options object, map is only accessible on desktop while holding Command/Ctrl and only accessible on mobile with two fingers. Interacting with the map using normal gestures will trigger an informational screen. With this option enabled, "drag to pitch" requires a three-finger gesture. Cooperative gestures are disabled when a map enters fullscreen using FullscreenControl. A valid options object includes the following properties to customize the text on the informational screen. The values below are the defaults.

          {
          windowsHelpText: "Use Ctrl + scroll to zoom the map",
          macHelpText: "Use ⌘ + scroll to zoom the map",
          mobileHelpText: "Use two fingers to move the map",
          }
          
options.crossSourceCollisions
default: true
If true , symbols from multiple sources can collide with each other during collision detection. If false , collision detection is run separately for the symbols in each source.
options.doubleClickZoom
default: true
If true , the "double click to zoom" interaction is enabled (see DoubleClickZoomHandler ).
options.dragPan
default: true
If true , the "drag to pan" interaction is enabled. An Object value is passed as options to DragPanHandler#enable .
options.dragRotate
default: true
If true , the "drag to rotate" interaction is enabled (see DragRotateHandler ).
options.elevation
default: 0
The elevation of the initial geographical centerpoint of the map, in meters above sea level. If elevation is not specified in the constructor options, it will default to 0.
options.fadeDuration
default: 300
Controls the duration of the fade-in/fade-out animation for label collisions, in milliseconds. This setting affects all symbol layers. This setting does not affect the duration of runtime styling transitions or raster tile cross-fading.
options.fitBoundsOptions A Map#fitBounds options object to use only when fitting the initial bounds provided above.
options.fullscreenControl MapTiler logo
default: false
If true , an FullscreenControl will be added to the map. Valid options are top-left , top-right , bottom-left , bottom-right .
options.geolocate MapTiler logo
(boolean | GeolocationType.POINT | GeolocationType.COUNTRY )?
default: false
Center map on the visitor's location by using the IP geolocation API. There are two strategies:
  • GeolocationType.POINT: centering the map on the actual visitor location, optionnaly using the zoom option (zoom level 13 if none is provided). As a more precise option, if the user has previously granted access to the browser location (more precise) then this is going to be used.
  • GeolocationType.COUNTRY: fitting the map view on the bounding box of the visitor's country. In this case, the zoom option, if provided, will be ignored
The geolocate options will not be taken into consideration in the following cases:
  • if the center options is provided, then it prevails
  • if the hash options is provided with the value true AND a location hash is already part of the URL. If hash is true but there is not yet a location hash in the URL, then the geolocation will work.
options.geolocateControl MapTiler logo
default: true
If true , an MaptilerGeolocateControl will be added to the map. Valid options are top-left , top-right , bottom-left , bottom-right .
options.hash
default: false
If true , the map's position (zoom, center latitude, center longitude, bearing, and pitch) will be synced with the hash fragment of the page's URL. For example, http://path/to/my/page.html#2.59/39.26/53.07/-24.1/60 . An additional string may optionally be provided to indicate a parameter-styled hash, e.g. http://path/to/my/page.html#map=2.59/39.26/53.07/-24.1/60&foo=bar , where foo is a custom parameter and bar is an arbitrary hash distinct from the map hash.
options.interactive
default: true
If false , no mouse, touch, or keyboard listeners will be attached to the map, so it will not respond to interaction.
options.keyboard
default: true
If true , keyboard shortcuts are enabled (see KeyboardHandler ).
options.locale
default: null
A patch to apply to the default localization table for UI strings, e.g. control tooltips. The locale object maps namespaced UI string IDs to translated strings in the target language; see src/ui/default_locale.js for an example with all supported string IDs. The object may specify all UI strings (thereby adding support for a new translation) or only a subset of strings (thereby patching the default translation table).
options.localIdeographFontFamily
default: 'sans-serif'
Defines a CSS font-family for locally overriding generation of glyphs in the 'CJK Unified Ideographs', 'Hiragana', 'Katakana' and 'Hangul Syllables' ranges. In these ranges, font settings from the map's style will be ignored, except for font-weight keywords (light/regular/medium/bold). Set to false , to enable font settings from the map's style for these glyph ranges. The purpose of this option is to avoid bandwidth-intensive glyph server requests. (See Use locally generated ideographs .)
options.logoPosition
default: 'bottom-left'
A string representing the position of the MapTiler wordmark on the map. Valid options are top-left , top-right , bottom-left , bottom-right .
options.maptilerLogo MapTiler logo
default: true
If true , the MapTiler logo will be shown. false will only work on premium accounts
options.maxBounds If set, the map will be constrained to the given bounds.
options.maxCanvasSize
default: [4096, 4096]
The canvas' width and height max size. The values are passed as an array where the first element is max width and the second element is max height. You shouldn't set this above WebGl MAX_TEXTURE_SIZE
options.maxPitch
default: 60
The maximum pitch of the map (0-180).
options.maxTileCacheSize
default: null
The maximum number of tiles stored in the tile cache for a given source. If omitted, the cache will be dynamically sized based on the current viewport.
options.maxTileCacheZoomLevels
default: 5
The maximum number of zoom levels for which to store tiles for a given source. Tile cache dynamic size is calculated by multiplying maxTileCacheZoomLevels with the approximate number of tiles in the viewport for a given source.
options.maxZoom
default: 22
The maximum zoom level of the map (0-24).
options.minimap MapTiler logo
default: false
If true , an MaptilerMinimapControl will be added to the map. Valid options are top-left , top-right , bottom-left , bottom-right . or MinimapOptionsInput
options.minPitch
default: 0
The minimum pitch of the map (0-85). Values greater than 60 degrees are experimental and may result in rendering issues. If you encounter any, please raise an issue with details in the MapLibre project.
options.minZoom
default: 0
The minimum zoom level of the map (0-24).
options.navigationControl MapTiler logo
default: true
If true , an MaptilerNavigationControl will be added to the map. Valid options are top-left , top-right , bottom-left , bottom-right .
options.pitchWithRotate
default: true
If false , the map's pitch (tilt) control with "drag to rotate" interaction will be disabled.
options.pixelRatio The pixel ratio. The canvas' width attribute will be container.clientWidth * pixelRatio and its height attribute will be container.clientHeight * pixelRatio . Defaults to devicePixelRatio if not specified.
options.projectionControl MapTiler logo
default: false
If true , an MaptilerProjectionControl will be added to the map. Valid options are top-left , top-right , bottom-left , bottom-right .
options.refreshExpiredTiles
default: true
If false , the map won't attempt to re-request tiles once they expire per their HTTP cacheControl / expires headers.
options.renderWorldCopies
default: true
If true , multiple copies of the world will be rendered side by side beyond -180 and 180 degrees longitude. If set to false :
  • When the map is zoomed out far enough that a single representation of the world does not fill the map's entire container, there will be blank space beyond 180 and -180 degrees longitude.
  • Features that cross 180 and -180 degrees longitude will be cut in two (with one portion on the right edge of the map and the other on the left edge of the map) at every zoom level.
options.roll MapTiler logo
default: 0
The initial roll angle of the map, measured in degrees counter-clockwise about the camera boresight. If roll is not specified in the constructor options, MapTiler SDK will look for it in the map's style object. If it is not specified in the style, either, it will default to 0.
options.rollEnabled MapTiler logo
default: false
If false, the map's roll control with "drag to rotate" interaction will be disabled.
options.scaleControl MapTiler logo
default: false
If true , an ScaleControl will be added to the map. Valid options are top-left , top-right , bottom-left , bottom-right .
options.scrollZoom
default: true
If true , the "scroll to zoom" interaction is enabled. An Object value is passed as options to ScrollZoomHandler#enable .
options.terrain MapTiler logo
default: false
If true , the map's loads a 3D terrain, based on a MapTiler "raster-dem" source.
options.terrainControl MapTiler logo
default: false
If true , an MaptilerTerrainControl will be added to the map. Valid options are top-left , top-right , bottom-left , bottom-right .
options.terrainExaggeration MapTiler logo
default: 1
3D terrain exaggeration factor.
options.touchPitch
default: true
If true , the "drag to pitch" interaction is enabled. An Object value is passed as options to TouchPitchHandler#enable .
options.touchZoomRotate
default: true
If true , the "pinch to rotate and zoom" interaction is enabled. An Object value is passed as options to TouchZoomRotateHandler#enable .
options.trackResize
default: true
If true , the map will automatically resize when the browser window resizes.
options.transformCameraUpdate
default: null
A callback run before the map's camera is moved due to user input or animation. The callback can be used to modify the new center, zoom, pitch and bearing. Expected to return an object containing center, zoom, pitch or bearing values to overwrite.
options.transformRequest
default: null
A callback run before the Map makes a request for an external URL. The callback can be used to modify the url, set headers, or set the credentials property for cross-origin requests. Expected to return an object with a url property and optionally headers and credentials properties.

Example

options.validateStyle
default: true
If false, style validation will be skipped. Useful in production environment.

Methods

Events

Reference documentation of MapTiler SDK JS, an extension of MapLibre GL JS