TomTom map object
Overview
TomTomMap wraps a MapLibre GL JS map: it loads a TomTom style with your API key, coordinates the user events of every module, and keeps the SDK's state through style switches. The MapLibre map stays fully exposed.
Initialization
Basic setup
import { TomTomConfig } from '@tomtom-org/maps-sdk/core';
import { TomTomMap } from '@tomtom-org/maps-sdk/map';
TomTomConfig.instance.put({ apiKey: 'YOUR_API_KEY' });
const map = new TomTomMap({
mapLibre: {
container: 'map-container',
center: [4.9041, 52.3676],
zoom: 12
},
style: 'standardDark',
language: 'en-US'
});
Constructor parameters
TomTomMap takes one TomTomMapParams object. Only mapLibre.container is required.
| Property | Default | Sets |
|---|---|---|
mapLibre |
— | MapLibre's map options (container, center, zoom, bounds, pitch, interactive…), typed MapLibreOptions |
style |
'standardLight' |
The map style: a standard style id, a standard style with the parts it includes, or a custom style |
events |
— | How user events are detected and timed, and the cursors they show: map-level event configuration |
apiKey, language, geopoliticalView, … |
The global configuration | Any global configuration property, for this map only |
- Properties given here override the global configuration set with
TomTomConfig.instance.put(). - The SDK sets MapLibre's
style,attributionControlandtransformRequestitself, soMapLibreOptionsleaves them out. To route TomTom requests through a proxy of your own, setcommonBaseURL.
Map styles
style picks the style the map opens with, and map.setStyle(style) switches it at runtime, carrying every module's data and configuration over by default. setStyle returns a promise that resolves once every module has restored itself, and getStyleInput() returns the style last given. The Map styles guide covers the standard styles, custom styles, style parts, keeping or dropping state and listening to style changes.
Event configuration
events configures how user events behave for every module — precision mode, hit-box padding, hover timing, cursors — and map.updateEventsConfig(config) changes it at runtime:
const map = new TomTomMap({
mapLibre: { container: 'map' },
events: { precisionMode: 'point-then-box', paddingBoxPx: 10, cursorOnHover: 'pointer' },
});
map.updateEventsConfig({ longHoverDelayOnStillMapMS: 600 });
Every option and its default: map-level event configuration. A module's own events overrides the cursor and hover settings for its features.
Accessing MapLibre
map.mapLibreMap is the MapLibre Map itself, so any MapLibre API, control or plugin works on a TomTom map:
import { NavigationControl } from 'maplibre-gl';
const map = new TomTomMap({ mapLibre: { container: 'map' } });
map.mapLibreMap.addControl(new NavigationControl(), 'top-right');
map.mapLibreMap.on('moveend', () => {
console.log('Camera:', map.mapLibreMap.getCenter());
});
Use it for controls, markers, popups, camera moves, tile overlays and third-party plugins. For your own GeoJSON data, use the Custom GeoJSON module, which keeps it on the map across style switches. About MapLibre lists which SDK feature replaces which raw MapLibre call.
Map readiness
map.mapReady is true once the style has loaded, and false again for the duration of every style switch.
- Module factories (
get(),create()) wait for the map themselves: call them right after constructing it. - For your own code, await
setStyle, or register a style change handler.
Map errors
The map reports its failures as a TomTomMapError, whose code is one of the codes service errors use, with sdkVersion for bug reports:
| When | How it arrives | code |
|---|---|---|
| The browser cannot create a WebGL2 context | The constructor throws | WEBGL_UNSUPPORTED |
| The style fails to load | setStyle rejects, and the error handlers get it |
The HTTP status's (API_KEY_REJECTED, RATE_LIMITED, …), else STYLE_LOAD_FAILED |
| A tile, sprite or glyph fails to load | The error handlers get it, with the source in sourceId |
The HTTP status's, NETWORK_ERROR with no response, else UNEXPECTED |
| The browser takes the WebGL context away | The error handlers get it | WEBGL_CONTEXT_LOST |
import { TomTomMap, TomTomMapError } from '@tomtom-org/maps-sdk/map';
let map;
try {
map = new TomTomMap({ mapLibre: { container: 'map' } });
} catch (error) {
if (error instanceof TomTomMapError && error.code === 'WEBGL_UNSUPPORTED') showStaticImage();
else throw error;
}
const removeErrorHandler = map.addErrorHandler((error) => {
if (error.code === 'API_KEY_REJECTED') showMessage('The map could not load: check the API key');
});
- While no handler is registered, each failure is logged with
console.error, as MapLibre does. - A failed tile does not stop the map: MapLibre keeps drawing the others, so log it rather than tearing the map down.
Language configuration
language sets the language of the map labels; map.setLanguage('fr-FR') changes it at runtime. Without one, each label is in its local language. Either survives every style switch: see Map language.
const map = new TomTomMap({
mapLibre: { container: 'map' },
language: 'fr-FR'
});
Geopolitical view
geopoliticalView sets whose perspective the map draws disputed borders and names from, and map.setGeopoliticalView(view) switches it at runtime. Set in the global configuration, it applies to services too: see Geopolitical views.
const map = new TomTomMap({
mapLibre: { container: 'map' },
geopoliticalView: 'IN'
});
map.setGeopoliticalView('PK');
API reference
Key methods summary
mapLibreMap: the underlying MapLibre mapmapReady: whether the style has loadedsetStyle(style, options?): switch the style; resolves once the modules are restoredgetStyleInput(): the style last givenaddStyleChangeHandler(handler): run code around a style switch; returns a function that unsubscribes itaddErrorHandler(handler): receive every map failure as aTomTomMapError; returns a function that unsubscribes itsetLanguage(language),setGeopoliticalView(geopoliticalView): relabel and redraw the map already on screenupdateEventsConfig(config): change the map-level event configurationgetBBox(): the visible area as[west, south, east, north]styleLightDarkTheme: whether the loaded style is'light'or'dark', to bind your own UI tomapColors: the map's semantic colours and accent, as the Styling Foundations module last published them
Next steps
- Modules: add functionality with modules
- Map styles: standard and custom styles, and switching them
- User interaction events: handle interactions
- Services quickstart: connect TomTom services