import type { WaypointLike } from '@tomtom-org/maps-sdk/core';
import { bboxFromGeoJSON, TomTomConfig } from '@tomtom-org/maps-sdk/core';
import { RoutingModule, TomTomMap } from '@tomtom-org/maps-sdk/map';
import { calculateRoute } from '@tomtom-org/maps-sdk/services';
import { API_KEY } from './config';
import './style.css';
TomTomConfig.instance.put({ apiKey: API_KEY, language: 'en-GB' });
type Tile = {
country: string;
note: string;
waypoints: WaypointLike[];
};
const TILES: Tile[] = [
{
country: 'Netherlands',
note: 'White disc, km/h',
waypoints: [
[4.9, 52.379],
[4.764, 52.309],
],
},
{
country: 'Sweden',
note: 'Yellow disc, km/h — as the Nordics post it',
waypoints: [
[18.058, 59.331],
[18.0, 59.362],
],
},
{
country: 'United States',
note: 'Plaque, mph — converted from the km/h the route reports',
waypoints: [
[-122.419, 37.775],
[-122.447, 37.708],
],
},
{
country: 'United Kingdom',
note: 'Disc, mph — the same face, the other unit',
waypoints: [
[-0.127, 51.507],
[-0.208, 51.465],
],
},
];
const tileElement = (tile: Tile, index: number): HTMLElement => {
const container = document.createElement('div');
container.className = 'tile';
container.innerHTML = `
<div class="tile-map" id="tile-map-${index}"></div>
<div class="tile-label">
<span class="tile-country">${tile.country}</span>
<span class="tile-note">${tile.note}</span>
</div>`;
return container;
};
const whenPainted = (map: TomTomMap): Promise<void> =>
new Promise<void>((resolve) => {
const mapLibreMap = map.mapLibreMap;
const settle = () => {
if (!mapLibreMap.loaded()) return;
mapLibreMap.off('idle', settle);
resolve();
};
mapLibreMap.on('idle', settle);
settle();
});
const showTile = async (tile: Tile, index: number): Promise<void> => {
const routes = await calculateRoute({ locations: tile.waypoints, sectionTypes: ['speedLimit', 'country'] });
const map = new TomTomMap({
mapLibre: {
container: `tile-map-${index}`,
bounds: bboxFromGeoJSON(routes),
fitBoundsOptions: { padding: 32 },
interactive: false,
},
});
map.mapLibreMap.resize();
const routingModule = await RoutingModule.create(map, {
summaryBubbles: { visible: false },
sections: { speedLimit: { sign: { minZoom: 10 } } },
});
await routingModule.showRoutes(routes);
await whenPainted(map);
};
(async () => {
const grid = document.querySelector('#ui-maps-container') as HTMLElement;
TILES.forEach((tile, index) => grid.appendChild(tileElement(tile, index)));
await Promise.allSettled(TILES.map((tile, index) => showTile(tile, index)));
grid.classList.add('ready');
})();