Add the ability to centre and zoom the map with URL params. #50

This commit is contained in:
Ian Renton
2026-04-05 10:42:01 +01:00
parent 06c16e2f1f
commit 60126b0010
11 changed files with 66 additions and 37 deletions

View File

@@ -210,6 +210,7 @@ function loadOptions() {
// to be called after these are set up, but if the URL params ask for "embedded mode", this will suppress
// loading settings, so this needs to be called before that.
loadURLParams();
loadMapURLParams();
// Load settings from settings storage now all the controls are available
loadSettings();
@@ -344,6 +345,30 @@ function enableWABWAIGrid(show) {
}
}
// Load map-specific URL parameters for center position and zoom level.
// These set Leaflet state directly rather than form controls, so they live here rather than in loadURLParams().
// If any parameter is applied, firstLoad is set to false so updateMap() does not override the position.
function loadMapURLParams() {
let params = new URLSearchParams(document.location.search);
let lat = parseFloat(params.get("map-center-lat"));
let lon = parseFloat(params.get("map-center-lon"));
let zoom = parseFloat(params.get("map-zoom"));
let hasLatLon = !isNaN(lat) && !isNaN(lon);
let hasZoom = !isNaN(zoom);
if (hasLatLon || hasZoom) {
if (hasLatLon && hasZoom) {
map.setView([lat, lon], zoom);
} else if (hasLatLon) {
map.setView([lat, lon], map.getZoom());
} else {
map.setZoom(zoom);
}
firstLoad = false;
}
}
// Set up the map
function setUpMap() {
// Create map
@@ -415,7 +440,8 @@ function setUpMap() {
backgroundTileLayer.bringToBack();
}
// Display a default view.
// Display a default view. This will only last until the spots are first loaded, at which point the map will zoom
// to the extent of ths spots.
map.setView([30, 0], 3);
}