From 1bb6f70c9269ba0178064c564150540b7757a838bb05ee1fb634e53dc4266023 Mon Sep 17 00:00:00 2001 From: K1 Date: Sat, 2 May 2026 18:35:58 +0200 Subject: [PATCH] map position in url Signed-off-by: K1 --- dve.js | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/dve.js b/dve.js index 287adf8..acf47bd 100644 --- a/dve.js +++ b/dve.js @@ -9,6 +9,7 @@ const zoom = 14; const mapCounts = [2, 4, 6, 9]; let syncing = false; +let urlUpdateTimer = null; let TILE_SERVERS = []; let maps = []; @@ -23,6 +24,39 @@ function fillDropdown(select) { } } +function readUrlPosition() { + const match = window.location.hash.match(/^#map=([0-9.]+)\/(-?[0-9.]+)\/(-?[0-9.]+)/); + + if (match) { + const z = parseFloat(match[1]); + const lat = parseFloat(match[2]); + const lng = parseFloat(match[3]); + + if (Number.isFinite(lat) && Number.isFinite(lng) && Number.isFinite(z)) { + return { + center: [lat, lng], + zoom: z + }; + } + } + + return { + center, + zoom + }; +} + +function writeUrlPosition(map) { + if (urlUpdateTimer) clearTimeout(urlUpdateTimer); + + urlUpdateTimer = setTimeout(() => { + const c = map.getCenter(); + const hash = "#map=" + map.getZoom() + "/" + c.lat.toFixed(5) + "/" + c.lng.toFixed(5); + + history.replaceState(null, "", hash); + }, 100); +} + function sync(source) { if (syncing) return; syncing = true; @@ -32,6 +66,8 @@ function sync(source) { } }); syncing = false; + + writeUrlPosition(source); } function setLayer(map, config, currentLayer) { @@ -85,6 +121,7 @@ function setMapCount(count) { } function init() { + const urlPosition = readUrlPosition(); const mapCountSelector = document.getElementById("mapCountSelector"); fillMapCountDropdown(mapCountSelector); @@ -92,7 +129,7 @@ function init() { const select = document.getElementById("select" + i); fillDropdown(select); - const map = L.map("map" + i, { center, zoom, zoomControl: false }); + const map = L.map("map" + i, { center: urlPosition.center, zoom: urlPosition.zoom, zoomControl: false }); map.on("move", () => sync(map)); map.on("zoom", () => sync(map));