import 'maplibre-gl/dist/maplibre-gl.css'; import './switcher/switcher.css'; import maplibregl from 'maplibre-gl'; import React, { useRef, useLayoutEffect, useEffect, useState, } from 'react'; import { SwitcherControl } from './switcher/switcher'; import deviceCategories from '../common/deviceCategories'; import { prepareIcon, loadImage } from './mapUtil'; import { styleLocationIq, styleCarto, styleOsm, styleGmapsStreets, styleGmapsSatellite, styleGmapsHybrid } from './mapStyles'; import { useAttributePreference } from '../common/preferences'; import { useTranslation } from '../LocalizationProvider'; const element = document.createElement('div'); element.style.width = '100%'; element.style.height = '100%'; export const map = new maplibregl.Map({ container: element, center: [-100.360, 23.191], zoom: 5 }); let ready = false; const readyListeners = new Set(); const addReadyListener = (listener) => { readyListeners.add(listener); listener(ready); }; const removeReadyListener = (listener) => { readyListeners.delete(listener); }; const updateReadyValue = (value) => { ready = value; readyListeners.forEach((listener) => listener(value)); }; const initMap = async () => { if (ready) return; if (!map.hasImage('background')) { const background = await loadImage('images/background.svg'); map.addImage('background', prepareIcon(background), { pixelRatio: window.devicePixelRatio, }); await Promise.all(deviceCategories.map(async (category) => { const results = []; results.push(loadImage(`images/icon/${category.toLowerCase()}.png`).then((icon) => { map.addImage(`${category.toLowerCase()}-map`, prepareIcon(background, icon, null), { pixelRatio: window.devicePixelRatio, }); })); await Promise.all(results); })); } updateReadyValue(true); }; map.addControl(new maplibregl.NavigationControl({ showCompass: false })); const switcher = new SwitcherControl( () => updateReadyValue(false), () => { const waiting = () => { if (!map.loaded()) { setTimeout(waiting, 2000); } else { initMap(); } }; waiting(); }, ); map.addControl(switcher); const Map = ({ children }) => { const containerEl = useRef(null); const t = useTranslation(); const [mapReady, setMapReady] = useState(false); const mapboxAccessToken = useAttributePreference('mapboxAccessToken'); useEffect(() => { maplibregl.accessToken = mapboxAccessToken; }, [mapboxAccessToken]); useEffect(() => { switcher.updateStyles([ { id: 'osm', title: t('mapOsm'), uri: styleOsm() }, { id: 'carto', title: t('mapCarto'), uri: styleCarto() }, { id: 'gmapsStreets', title: t('mapGmapsStreets'), uri: styleGmapsStreets() }, { id: 'gmapsSatellite', title: t('mapGmapsSatellite'), uri: styleGmapsSatellite() }, { id: 'gmapsHybrid', title: t('mapGmapsHybrid'), uri: styleGmapsHybrid() }, ], 'gmapsStreets'); }, []); useEffect(() => { const listener = (ready) => setMapReady(ready); addReadyListener(listener); return () => { removeReadyListener(listener); }; }, []); useLayoutEffect(() => { const currentEl = containerEl.current; currentEl.appendChild(element); map.resize(); return () => { currentEl.removeChild(element); }; }, [containerEl]); return (
{mapReady && children}
); }; export default Map;