Events
The TomTom Maps SDK provides three distinct event systems, each addressing a different layer of interaction:
| Category | What it covers | Entry point |
|---|---|---|
| User interaction events | Click, hover, long-hover, and contextmenu on map features | module.events.on('click', ...) |
| Module lifecycle events | Config changes and feature rendering inside a module | module.events.on('config-change', ...) |
| Style change events | Map style transitions (setStyle) | map.addStyleChangeHandler(...) |
User interaction events
User interaction events fire when the map’s end users interact with features rendered by a module — clicks, hovers, long-hovers, and right-clicks. The SDK enhances MapLibre’s raw mouse events with built-in hover state management, configurable precision, and automatic mapping back to the original typed data.
// Most modules expose user interaction events via events.on(...)placesModule.events.on('click', (feature, lngLat) => { /* ... */ });trafficModule.events.on('hover', (feature, lngLat) => { /* ... */ });baseMap.events.on('contextmenu', (feature, lngLat) => { /* ... */ });
// RoutingModule is namespaced differentlyrouting.events.user.click((feature, lngLat) => { /* ... */ });User Interaction Events guide →
Module lifecycle events
Module lifecycle events fire when a module’s internal state changes — either because configuration was updated or because new features were rendered on the map. These events are independent of any user gesture.
config-change— emitted by every module whenever its config is mutated (viaapplyConfig,setVisible, or any module-specific setter).shown-features— emitted by modules that have ashowmethod (PlacesModule, GeometriesModule, RoutingModule, TrafficAreaAnalyticsModule), immediately after features are displayed.
// Fires on any config mutationtrafficFlow.events.on('config-change', (config) => { syncToggle(config?.visible);});
// Fires after places.show() completesplaces.events.on('shown-features', (features) => { fitMapToResults(features);});Module Lifecycle Events guide →
Style change events
Style change events let you hook into the setStyle transition lifecycle — before the old style is torn down and after the new style is fully loaded. Use these to preserve custom MapLibre layers, sync UI state (e.g. dark mode), or run async teardown/setup logic.
map.addStyleChangeHandler({ onStyleAboutToChange: () => { saveCustomLayers(); }, onStyleChanged: () => { restoreCustomLayers(); syncDarkModeClass(); },});MapLibre events
Because TomTomMap exposes the underlying MapLibre instance via map.mapLibreMap, the full MapLibre Map event API is also available — load, moveend, zoom, styledata, and many more.
map.mapLibreMap.on('moveend', () => { console.log('Viewport changed:', map.mapLibreMap.getBounds());});Prefer the SDK’s own event systems for anything involving SDK-managed features. Use map.mapLibreMap events for low-level map lifecycle hooks that the SDK does not expose directly.
Choosing the right event
| Scenario | Use |
|---|---|
Low-level map lifecycle (load, zoom, moveend) | MapLibre event (map.mapLibreMap.on(...)) |
| Respond when a user clicks a place, route, or traffic feature | User interaction event ('click') |
| Respond when a user hovers over map features | User interaction event ('hover' / 'long-hover') |
| Detect clicks on the background map (outside features) | BaseMapModule user event |
| Keep external UI in sync with a module’s config | Module lifecycle event ('config-change') |
| React after new data is displayed on the map | Module lifecycle event ('shown-features') |
| Preserve custom layers when the map style changes | Style change handler (addStyleChangeHandler) |
| Update UI for light/dark style switch | Style change handler (addStyleChangeHandler) |