Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146import { 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<StopKind, string> = { 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() }, }}