This is the documentation of version 0.59.0. Go to the latest version, 1.0.0-rc.5.
TomTom Maps for JavaScript
    Preparing search index...

    Class StylingModule

    Semantic styling of the base map, traffic and POIs — the tier between hiding a layer group and hand-editing MapLibre layers.

    Every adjustable thing is a knob with a stable id, a kind, a range and a default read from the loaded style. The ids form the StylingKnobId union, and stylingKnobCatalogue lists each one's kind, range and description as data, with no map needed to read it.

    Which styles a knob works on is appliesTo in the catalogue. any-style knobs (sizes, widths) wrap whatever the style already does — text-size: E becomes ["*", 1.3, E] — so they work on a custom style too. tomtom-styles knobs (toggles, colours) reach their layers through the TomTom styles this SDK version is verified against; on any other style setting one warns once and does nothing, rather than failing silently.

    Style changes. Settings are re-applied after every setStyle that carries state over, on top of whatever the other modules restored, and dropped on a clean switch (resetState: true).

    Interplay with other modules. The base-map group toggles (basemap.*, buildings.*, roads.shields) write through BaseMapModule and get() reads their layers live, so it agrees with isVisible. The later call wins: a BaseMapModule.setVisible that covers a group clears its toggle from these settings, and a style change re-applies a toggle still set.

    The toggles within a module's layers (pois.microMarkers, roads.arrows, roads.restricted, roads.underConstruction, roads.exitNumbers) only hide: false hides those layers, true never shows them while POIsModule or BaseMapModule hides them, and unsetting one leaves the layers as their module has them.

    pois.zoomShift rewrites the POI layer filters, which POIsModule.filterCategories also narrows: the two compose, so either can be set at any time and neither drops the other.

    import { StylingModule } from '@tomtom-org/maps-sdk/map';

    const styling = await StylingModule.get(map);
    styling.set('labels.sizeFactor', 1.2);
    styling.set('roads.exitNumbers', false);
    styling.set('traffic.flow.slowColor', '#f59e0b');

    // The catalogue as data, and each knob's value in force:
    for (const knob of stylingKnobCatalogue) {
    console.log(knob.id, knob.kind, knob.range, styling.get(knob.id));
    }

    // Serializable, diffable, re-applicable:
    const saved = styling.getConfig();
    styling.resetConfig();
    styling.applyConfig(saved);

    Hierarchy (View Summary)

    Index
    • get events(): StylingEvents

      Lifecycle events of this module. The module owns no map features, so there are no user interaction events; config-change fires with the full settings after every knob change.

      Returns StylingEvents

      styling.events.on('config-change', (settings) => localStorage.setItem('styling', JSON.stringify(settings)));
      
    • get layers(): { query: (query: LayerQuery) => LayerSelection }

      The advanced tier: raw MapLibre paint, layout and filter edits over a query of style layers, re-applied across style switches. Version-coupled by nature — see LayerSelection for the terms — so prefer a knob or setMapColors wherever one covers the intent.

      Returns { query: (query: LayerQuery) => LayerSelection }

      styling.layers.query({ group: 'roadLabels' }).setPaint({ 'text-color': '#93c5fd' });
      
    • get sourceAndLayerIDs(): Record<keyof SOURCES_WITH_LAYERS, SourceWithLayerIDs>

      Gets the source and layer identifiers for all sources managed by this module.

      This property provides access to the MapLibre source and layer IDs that were created and are managed by this module. These IDs can be used to interact directly with MapLibre's API or to identify which layers belong to this module.

      Returns Record<keyof SOURCES_WITH_LAYERS, SourceWithLayerIDs>

      A record mapping each source name to its corresponding source ID and layer IDs. Each entry contains the MapLibre source identifier and an array of layer identifiers associated with that source.

      The returned IDs are useful when you need to:

      • Directly manipulate layers using MapLibre's native API
      • Identify which layers on the map belong to this module
      • Set layer ordering or positioning relative to other layers
      • Access source or layer properties through MapLibre methods
      const ids = myModule.sourceAndLayerIDs;
      console.log(ids);
      // {
      // mySource: {
      // sourceID: 'my-source-id',
      // layerIDs: ['layer-1', 'layer-2']
      // }
      // }

      // Use with MapLibre API
      const map = myModule.mapLibreMap;
      ids.mySource.layerIDs.forEach(layerId => {
      map.setLayoutProperty(layerId, 'visibility', 'visible');
      });
    • Applies a configuration to this module.

      This method updates the module's behavior and appearance based on the provided configuration. The configuration is stored internally and will be automatically reapplied if the map style changes.

      Parameters

      • config: StylingSettings | undefined

        The configuration object to apply to the module. Pass undefined to reset the configuration to default values.

      Returns void

      When a configuration is applied, the module updates its visual representation and behavior accordingly. The configuration persists across map style changes, ensuring consistent module behavior even when the map's base style is modified.

      Unless a module documents otherwise, the given configuration replaces the current one rather than being merged into it. To change one part of it, use updateConfig.

      // Apply a new configuration
      myModule.applyConfig({ visible: true, opacity: 0.8 });

      // Reset to default configuration
      myModule.applyConfig(undefined);
      • resetConfig for a convenience method to reset configuration
      • getConfig to retrieve the current configuration
    • Applies a preset: a named bundle of knob settings the SDK ships for a common intent (data-viz, night-driving, minimal, globe). See stylingPresetCatalogue for what each one sets.

      Parameters

      • id: "globe" | "data-viz" | "night-driving" | "minimal"

        The preset to apply.

      • options: { merge?: boolean } = {}

        merge: true lays the preset over the current settings instead of replacing them (the default), so knobs the preset does not mention keep their values.

      Returns void

      Error for an unknown preset id.

      styling.applyPreset('data-viz');
      styling.set('labels.sizeFactor', 1.1); // then adjust

      styling.applyPreset('globe', { merge: true }); // keep the rest, switch to a globe with a sky
    • The style as currently rendered — the loaded style with every knob applied — as a MapLibre style specification, to save, diff, serve yourself, or load back as a custom style.

      Returns StyleSpecification

      It includes whatever the SDK's data modules (places, routes) have on the map, and its tile URLs carry the API key the map was created with: strip or rotate it before publishing.

    • The value a knob currently has: the override if one is set, otherwise the style's own default. A base-map group toggle reads its layers instead, so it also reflects BaseMapModule.setVisible.

      Type Parameters

      • ID extends
            | "pois.minZoom"
            | "pois.zoomShift"
            | "colors.accent"
            | "basemap.ferries"
            | "basemap.land"
            | "basemap.water"
            | "basemap.roads"
            | "basemap.railways"
            | "basemap.borders"
            | "basemap.natureLabels"
            | "basemap.roadLabels"
            | "basemap.houseNumbers"
            | "basemap.smallerTownLabels"
            | "basemap.stateLabels"
            | "basemap.cityLabels"
            | "basemap.allPlaceLabels"
            | "basemap.capitalLabels"
            | "basemap.countryLabels"
            | "hillshade.method"
            | "hillshade.lightDirection"
            | "hillshade.exaggeration"
            | "hillshade.maxZoom"
            | "view.projection"
            | "view.sky"
            | "view.skyColor"
            | "view.horizonColor"
            | "view.spaceColor"
            | "hillshade.shadowColor"
            | "hillshade.highlightColor"
            | "hillshade.accentColor"
            | "labels.sizeFactor"
            | "symbols.sizeFactor"
            | "roads.widthFactor"
            | "pois.sizeFactor"
            | "pois.labelColor"
            | "pois.labelOutlineColor"
            | "pois.microMarkers"
            | "roads.exitNumbers"
            | "roads.shields"
            | "roads.arrows"
            | "roads.restricted"
            | "roads.underConstruction"
            | "buildings.footprints"
            | "buildings.3d"
            | "traffic.flow.freeColor"
            | "traffic.flow.slowColor"
            | "traffic.flow.queueingColor"
            | "traffic.flow.stationaryColor"
            | "traffic.flow.closedColor"
            | "traffic.flow.widthFactor"
            | "traffic.incidents.minorColor"
            | "traffic.incidents.moderateColor"
            | "traffic.incidents.majorColor"
            | "traffic.incidents.closedColor"
            | "traffic.incidents.widthFactor"
            | "colors.land"
            | "colors.water"
            | "colors.vegetation"
            | "colors.park"
            | "colors.artificial"
            | "colors.roadMajor"
            | "colors.road"
            | "colors.roadOutline"
            | "colors.label"
            | "colors.labelOutline"

      Parameters

      • id: ID

        The knob to read.

      Returns StylingKnobValueOf<ID> | undefined

      The current value, or undefined when the style defines it as a data-driven expression that no single value stands for.

      styling.get('labels.sizeFactor'); // 1 until set
      
    • Retrieves a copy of the current module configuration.

      This method returns a shallow copy of the configuration object that is currently applied to the module. If no configuration has been applied, it returns undefined.

      Returns NonNullable<StylingSettings> | undefined

      A shallow copy of the current configuration object, or undefined if no configuration is currently applied. The returned object is a copy to prevent unintended modifications to the internal state.

      The returned configuration object is a shallow copy, which means that while the top-level properties are copied, any nested objects or arrays are still referenced from the original configuration. This is sufficient for most use cases but should be kept in mind when dealing with complex configurations.

      // Apply a configuration
      myModule.applyConfig({ visible: true, opacity: 0.8 });

      // Later, retrieve the current configuration
      const currentConfig = myModule.getConfig();
      console.log(currentConfig); // { visible: true, opacity: 0.8 }

      // When no config is applied
      myModule.resetConfig();
      console.log(myModule.getConfig()); // undefined
    • Restores the style's own values for one knob, or for all of them.

      Parameters

      • Optionalid:
            | "pois.minZoom"
            | "pois.zoomShift"
            | "colors.accent"
            | "basemap.ferries"
            | "basemap.land"
            | "basemap.water"
            | "basemap.roads"
            | "basemap.railways"
            | "basemap.borders"
            | "basemap.natureLabels"
            | "basemap.roadLabels"
            | "basemap.houseNumbers"
            | "basemap.smallerTownLabels"
            | "basemap.stateLabels"
            | "basemap.cityLabels"
            | "basemap.allPlaceLabels"
            | "basemap.capitalLabels"
            | "basemap.countryLabels"
            | "hillshade.method"
            | "hillshade.lightDirection"
            | "hillshade.exaggeration"
            | "hillshade.maxZoom"
            | "view.projection"
            | "view.sky"
            | "view.skyColor"
            | "view.horizonColor"
            | "view.spaceColor"
            | "hillshade.shadowColor"
            | "hillshade.highlightColor"
            | "hillshade.accentColor"
            | "labels.sizeFactor"
            | "symbols.sizeFactor"
            | "roads.widthFactor"
            | "pois.sizeFactor"
            | "pois.labelColor"
            | "pois.labelOutlineColor"
            | "pois.microMarkers"
            | "roads.exitNumbers"
            | "roads.shields"
            | "roads.arrows"
            | "roads.restricted"
            | "roads.underConstruction"
            | "buildings.footprints"
            | "buildings.3d"
            | "traffic.flow.freeColor"
            | "traffic.flow.slowColor"
            | "traffic.flow.queueingColor"
            | "traffic.flow.stationaryColor"
            | "traffic.flow.closedColor"
            | "traffic.flow.widthFactor"
            | "traffic.incidents.minorColor"
            | "traffic.incidents.moderateColor"
            | "traffic.incidents.majorColor"
            | "traffic.incidents.closedColor"
            | "traffic.incidents.widthFactor"
            | "colors.land"
            | "colors.water"
            | "colors.vegetation"
            | "colors.park"
            | "colors.artificial"
            | "colors.roadMajor"
            | "colors.road"
            | "colors.roadOutline"
            | "colors.label"
            | "colors.labelOutline"

        The knob to reset; omit to reset every knob (same as resetConfig).

      Returns void

      styling.reset('labels.sizeFactor');
      styling.reset(); // back to the style as shipped
    • Resets the configuration of this module to its default values.

      This is a convenience method that clears any previously applied configuration and restores the module to its initial state. This is equivalent to calling applyConfig(undefined).

      Returns void

      After calling this method, the module will behave as if no configuration was ever applied. Any custom settings, styling, or behavior modifications will be removed and replaced with default values.

      // Apply some configuration
      myModule.applyConfig({ visible: true, opacity: 0.5 });

      // Later, reset to defaults
      myModule.resetConfig();
      • applyConfig to apply a new configuration
      • getConfig to retrieve the current configuration before resetting
    • Sets one knob. The value is validated against the knob's kind and range before anything is touched, and applied to every layer the knob reaches.

      Type Parameters

      • ID extends
            | "pois.minZoom"
            | "pois.zoomShift"
            | "colors.accent"
            | "basemap.ferries"
            | "basemap.land"
            | "basemap.water"
            | "basemap.roads"
            | "basemap.railways"
            | "basemap.borders"
            | "basemap.natureLabels"
            | "basemap.roadLabels"
            | "basemap.houseNumbers"
            | "basemap.smallerTownLabels"
            | "basemap.stateLabels"
            | "basemap.cityLabels"
            | "basemap.allPlaceLabels"
            | "basemap.capitalLabels"
            | "basemap.countryLabels"
            | "hillshade.method"
            | "hillshade.lightDirection"
            | "hillshade.exaggeration"
            | "hillshade.maxZoom"
            | "view.projection"
            | "view.sky"
            | "view.skyColor"
            | "view.horizonColor"
            | "view.spaceColor"
            | "hillshade.shadowColor"
            | "hillshade.highlightColor"
            | "hillshade.accentColor"
            | "labels.sizeFactor"
            | "symbols.sizeFactor"
            | "roads.widthFactor"
            | "pois.sizeFactor"
            | "pois.labelColor"
            | "pois.labelOutlineColor"
            | "pois.microMarkers"
            | "roads.exitNumbers"
            | "roads.shields"
            | "roads.arrows"
            | "roads.restricted"
            | "roads.underConstruction"
            | "buildings.footprints"
            | "buildings.3d"
            | "traffic.flow.freeColor"
            | "traffic.flow.slowColor"
            | "traffic.flow.queueingColor"
            | "traffic.flow.stationaryColor"
            | "traffic.flow.closedColor"
            | "traffic.flow.widthFactor"
            | "traffic.incidents.minorColor"
            | "traffic.incidents.moderateColor"
            | "traffic.incidents.majorColor"
            | "traffic.incidents.closedColor"
            | "traffic.incidents.widthFactor"
            | "colors.land"
            | "colors.water"
            | "colors.vegetation"
            | "colors.park"
            | "colors.artificial"
            | "colors.roadMajor"
            | "colors.road"
            | "colors.roadOutline"
            | "colors.label"
            | "colors.labelOutline"

      Parameters

      • id: ID

        The knob to set; see stylingKnobCatalogue for the catalogue.

      • value: StylingKnobValueOf<ID>

        A number for factor/number knobs (within range), a boolean for toggle knobs, a CSS colour string for color knobs.

      Returns void

      RangeError when the value is not valid for the knob; Error for an unknown id.

      styling.set('roads.widthFactor', 1.3);
      styling.set('buildings.3d', true);
      styling.set('traffic.flow.stationaryColor', 'hsl(0, 100%, 40%)');
    • Recolours the whole base map with up to ten semantic colours in one call — the style themes' Foundations palette, each listed with what it reaches under MapColorName — and sets accent, the default colour of routes, geometries and places (TomTomMap.mapColors).

      Partial objects are fine; colours not given stay as they are, except on the mono styles, which derive the other nine from land as their theme does: there the theme's own formulas re-derive them from the land given.

      Parameters

      • colors: MapColors

        The colours to set, as CSS colour strings.

      Returns void

      Each colour re-derives every shade the style built from it — a trunk variant, an outline, a tunnel — at the style's own HSL offset, so those relationships survive the recolour. The colours are also the colors.* knobs, so get, reset and settings persistence apply.

      Those offsets come off the loaded style, which makes a palette a recolour and not an inversion: dark land over standardLight pushes the derived shades to the ends of their range, so switch to standardDark first.

      RangeError for a value that is not a colour; Error for an unknown colour name.

      styling.setMapColors({ land: '#f3f5f7', water: '#accbe2', roadMajor: '#a1b8ce', label: '#364659' });
      styling.reset('colors.roadMajor'); // one colour back to the style
      styling.setMapColors(deriveMapColors(['#0b2a3a', '#0fa8c8', '#f6efe3'])); // map-theme plugin assigns the roles
    • Changes part of the configuration and keeps the rest.

      The given properties are laid over the current configuration — one level deep, so a nested object you pass replaces the nested object that was there — and the result is applied with applyConfig. This is the same on every module, whatever its applyConfig does with a whole configuration.

      Parameters

      • partial: Partial<NonNullable<CFG>>

        The properties to change.

      Returns void

      routing.applyConfig({ displayUnits: 'imperial', theme: { mainColor: '#0a3653' } });
      routing.updateConfig({ theme: { mainColor: '#c2185b' } }); // displayUnits stays imperial

      applyConfig to apply a whole configuration

    • Retrieves the styling module of the given map, creating it on first use.

      Parameters

      • map: TomTomMap

        The TomTomMap instance to style.

      • Optionalsettings: StylingSettings

        Optional knob values to apply right away, e.g. restored from storage.

      Returns Promise<StylingModule>

      A promise that resolves once the map style is loaded and the settings are applied.

      Instances: the module styles the sources and layers the map style already provides, so every map has exactly one — a second get() returns the same instance, with the given settings applied on top of what it already holds.

      const styling = await StylingModule.get(map, { 'labels.sizeFactor': 1.2 });