import { TomTomConfig } from '@tomtom-org/maps-sdk/core';
import { BaseMapModule, PlacesModule, TomTomMap } from '@tomtom-org/maps-sdk/map';
import { reverseGeocode } from '@tomtom-org/maps-sdk/services';
import { LngLat } from 'maplibre-gl';
import { clearConnectingLine, initConnectingLine, updateConnectingLine } from './connectingLine';
import './style.css';
import { API_KEY } from './config';
TomTomConfig.instance.put({ apiKey: API_KEY });
(async () => {
const map = new TomTomMap({
mapLibre: {
container: 'sdk-map',
center: [4.8896, 52.37325],
zoom: 20,
},
style: 'monoLight',
});
await map.mapLibreMap.once('styledata');
initConnectingLine(map.mapLibreMap);
const clickedPlace = await PlacesModule.get(map, { icon: { default: { style: { fillColor: '#ffffff' } } } });
const revGeoPlace = await PlacesModule.get(map, { icon: { default: { style: { fillColor: '#df1b12' } } } });
let isMarkerVisible = false;
const removeMarkers = () => {
revGeoPlace.clear();
clickedPlace.clear();
clearConnectingLine(map.mapLibreMap);
isMarkerVisible = false;
};
const onMapClick = async (_: any, clickedLngLat: LngLat) => {
if (isMarkerVisible) {
removeMarkers();
} else {
const clickedPosition = clickedLngLat.toArray();
await clickedPlace.show({
type: 'Feature',
geometry: { type: 'Point', coordinates: clickedPosition },
id: 'clicked-point',
properties: {
type: 'Point Address',
address: {
freeformAddress: `Clicked on\n${clickedLngLat.lng.toFixed(5)}, ${clickedLngLat.lat.toFixed(5)}`,
},
},
});
const revGeoResult = await reverseGeocode({ position: clickedPosition });
await revGeoPlace.show({
...revGeoResult,
geometry: {
...revGeoResult.geometry,
coordinates: revGeoResult.properties.originalPosition,
},
});
updateConnectingLine(map.mapLibreMap, [clickedPosition, revGeoResult.properties.originalPosition]);
isMarkerVisible = true;
}
};
const basemap = await BaseMapModule.get(map);
basemap.events.on('click', onMapClick);
await onMapClick(undefined, new LngLat(4.8896, 52.37321));
})();