Add position to url

This commit is contained in:
2024-03-19 22:27:23 +01:00
parent 22906fb7a2
commit ed83bb4e16
+20 -2
View File
@@ -46,10 +46,22 @@ const buffers: { [reason in BannedArea]: (FeatureCollection | Feature)[] } = {
[BannedAreas.OTHER]: [] [BannedAreas.OTHER]: []
}; };
const center = [53.0711829, 8.8087718] as [number, number]; let center: [number, number] = [53.0711829, 8.8087718];
let zoom = 14;
const urlPosition = /#@(\d+.?\d*),(\d+.?\d*),(\d+)/.exec(window.location.hash);
if (urlPosition && urlPosition.length === 4) {
const lat = parseFloat(urlPosition[1]);
const lon = parseFloat(urlPosition[2]);
const z = parseInt(urlPosition[3]);
if (!isNaN(lat) && !isNaN(lon) && !isNaN(z)) {
center = [lat, lon];
zoom = z;
}
}
const map = new LMap('map', { const map = new LMap('map', {
center, center,
zoom: 14, zoom,
zoomControl: true, zoomControl: true,
layers: [m_mono, exclusionZone, ...Object.values(features)] layers: [m_mono, exclusionZone, ...Object.values(features)]
}); });
@@ -66,6 +78,12 @@ new Popup()
.addTo(map); .addTo(map);
map.on('moveend', async () => { map.on('moveend', async () => {
const center = map.getCenter();
window.history.pushState(
{},
'',
`${import.meta.env.BASE_URL || '/'}#@${center.lat.toFixed(4)},${center.lng.toFixed(4)},${map.getZoom()}`
);
await fetchData(); await fetchData();
}); });
let loading = false; let loading = false;