import { defaults as defaultControls } from 'ol/control/defaults.js' import Feature from 'ol/Feature.js' import { LineString, Point } from 'ol/geom.js' import TileLayer from 'ol/layer/Tile.js' import VectorLayer from 'ol/layer/Vector.js' import OlMap from 'ol/Map.js' import { fromLonLat, transformExtent } from 'ol/proj.js' import OSM from 'ol/source/OSM.js' import VectorSource from 'ol/source/Vector.js' import { Circle as CircleStyle, Fill, Stroke, Style, Text } from 'ol/style.js' import View from 'ol/View.js' import 'ol/ol.css' /** France métropolitaine, Corse comprise (lon/lat). */ const FRANCE_EXTENT = transformExtent([-5.4, 41.2, 9.8, 51.3], 'EPSG:4326', 'EPSG:3857') const FRANCE_CENTER = fromLonLat([2.45, 46.6]) /** Bleu des panneaux d'autoroute, cerné de blanc pour tenir sur tous les fonds. */ const ROUTE_STYLE = [ new Style({ stroke: new Stroke({ color: '#ffffff', width: 9, lineCap: 'round', lineJoin: 'round' }), }), new Style({ stroke: new Stroke({ color: '#0f4a9b', width: 5, lineCap: 'round', lineJoin: 'round' }), }), ] /** Mêmes couleurs que les pastilles du formulaire : le repère confirme la ligne saisie. */ const STOP_COLOR: Record = { start: '#006b54', via: '#0f4a9b', direction: '#0f4a9b', end: '#a8321f', } /** * Le point de passage se dessine évidé : on le longe, on n'y fait pas halte. * La forme suffit à l'en distinguer, sans lui inventer une couleur qui le * ferait passer pour autre chose qu'un bleu de balisage. */ function markerStyle(label: string, kind: StopKind) { const color = STOP_COLOR[kind] const hollow = kind === 'direction' return new Style({ image: new CircleStyle({ radius: hollow ? 9 : 11, fill: new Fill({ color: hollow ? '#ffffff' : color }), stroke: new Stroke({ color: hollow ? color : '#ffffff', width: 2.5 }), }), text: new Text({ text: label, fill: new Fill({ color: hollow ? color : '#ffffff' }), font: "700 12px 'Archivo Variable', system-ui, sans-serif", offsetY: 1, }), }) } export type StopKind = 'start' | 'via' | 'direction' | 'end' /** Une adresse confirmée : sa position, et la pastille qui la désigne dans le formulaire. */ export type Stop = { lon: number; lat: number; label: string; kind: StopKind } export type RouteMap = { /** * Pose les repères des adresses déjà confirmées, et recadre dessus dès que * leurs positions changent : la carte répond à la saisie, sans attendre le calcul. */ showStops(stops: Stop[]): void /** Affiche le tracé, puis recadre la vue dessus. */ showRoute(coordinates: [number, number][]): void /** Efface le tracé ; les repères, eux, restent : ils décrivent le formulaire. */ clear(): void } export function createMap(target: HTMLElement): RouteMap { const routeSource = new VectorSource() const markerSource = new VectorSource() const map = new OlMap({ target, controls: defaultControls({ attributionOptions: { collapsible: false } }), layers: [ // Fond de plan OpenStreetMap, celui livré par défaut avec OpenLayers. new TileLayer({ source: new OSM() }), new VectorLayer({ source: routeSource, style: ROUTE_STYLE }), new VectorLayer({ source: markerSource }), ], view: new View({ center: FRANCE_CENTER, zoom: 5.5, maxZoom: 19 }), }) // La vue par défaut cadre la France entière : `fit` a besoin de la taille du // conteneur, connue seulement après le premier rendu. map.once('postrender', () => { if (!markerSource.isEmpty()) return map.getView().fit(FRANCE_EXTENT, { padding: [24, 24, 24, 24] }) }) /** * Positions des repères déjà cadrés. Renommer une étape ou en changer le rang * n'y touche pas : la vue ne bouge que si un point a bougé. */ let fitted = '' return { showStops(stops) { markerSource.clear() for (const stop of stops) { const feature = new Feature(new Point(fromLonLat([stop.lon, stop.lat]))) feature.setStyle(markerStyle(stop.label, stop.kind)) markerSource.addFeature(feature) } const positions = stops.map((s) => `${s.lon},${s.lat}`).join(';') if (positions === fitted) return fitted = positions const extent = markerSource.getExtent() if (!extent) return // Une adresse seule n'a pas d'étendue : `fit` s'arrête alors au zoom // maximal demandé, assez large pour montrer la ville autour du point. map.getView().fit(extent, { padding: [60, 60, 60, 60], duration: 500, maxZoom: 13, }) }, showRoute(coordinates) { routeSource.clear() const line = new LineString(coordinates.map((c) => fromLonLat(c))) routeSource.addFeature(new Feature(line)) map.getView().fit(line.getExtent(), { padding: [60, 60, 60, 60], duration: 500, maxZoom: 16, }) }, clear() { routeSource.clear() }, } }