2026-03-20 19:16:54 +01:00
/* global L */
2026-03-20 18:35:54 +01:00
export function initEventMap ( ) {
const mapEl = document . getElementById ( 'event-map' )
if ( ! mapEl ) return
const address = mapEl . dataset . address
if ( ! address ) return
const link = document . createElement ( 'link' )
link . rel = 'stylesheet'
2026-03-20 18:38:07 +01:00
link . href = 'https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.min.css'
2026-03-20 18:35:54 +01:00
document . head . appendChild ( link )
const script = document . createElement ( 'script' )
2026-03-20 18:38:07 +01:00
script . src = 'https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.min.js'
2026-03-20 18:35:54 +01:00
script . onload = ( ) => geocodeAndRender ( address , mapEl )
document . head . appendChild ( script )
}
function geocodeAndRender ( address , mapEl ) {
const url = 'https://nominatim.openstreetmap.org/search?format=json&limit=1&q=' + encodeURIComponent ( address )
globalThis . fetch ( url )
. then ( r => r . json ( ) )
. then ( data => {
if ( data . length === 0 ) {
mapEl . innerHTML = '<div style="display:flex;align-items:center;justify-content:center;height:100%;background:#f3f4f6;"><p style="font-weight:700;color:#9ca3af;">Adresse introuvable sur la carte</p></div>'
return
}
2026-03-20 21:25:53 +01:00
const lat = Number . parseFloat ( data [ 0 ] . lat )
const lon = Number . parseFloat ( data [ 0 ] . lon )
2026-03-20 18:35:54 +01:00
const map = L . map ( mapEl ) . setView ( [ lat , lon ] , 16 )
2026-03-20 18:37:32 +01:00
L . tileLayer ( 'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png' , {
attribution : '© <a href="https://www.openstreetmap.org/copyright">OSM</a> © <a href="https://carto.com/">CARTO</a>' ,
2026-03-20 18:35:54 +01:00
maxZoom : 19 ,
2026-03-20 18:37:32 +01:00
subdomains : 'abcd' ,
2026-03-20 18:35:54 +01:00
} ) . addTo ( map )
2026-03-20 18:46:39 +01:00
const icon = L . icon ( {
iconUrl : '/marker.png' ,
iconSize : [ 40 , 40 ] ,
iconAnchor : [ 20 , 40 ] ,
popupAnchor : [ 0 , - 40 ] ,
2026-03-20 18:48:01 +01:00
className : 'leaflet-marker-custom' ,
2026-03-20 18:46:39 +01:00
} )
L . marker ( [ lat , lon ] , { icon } ) . addTo ( map ) . bindPopup ( '<b>' + address + '</b>' ) . openPopup ( )
2026-03-20 18:35:54 +01:00
} )
. catch ( ( ) => {
mapEl . innerHTML = '<div style="display:flex;align-items:center;justify-content:center;height:100%;background:#f3f4f6;"><p style="font-weight:700;color:#9ca3af;">Carte indisponible</p></div>'
} )
}