From eeca96f21b31bab30fa6a642b7d98c7a37be1240 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Sun, 28 Jan 2024 02:21:10 +0100 Subject: Put basemap name in location hash. --- main.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/main.js b/main.js index b752e3e..28c885f 100644 --- a/main.js +++ b/main.js @@ -87,10 +87,7 @@ const extent = [110720, 6927136, 1159296, 7975712]; * https://www.lantmateriet.se/sv/om-lantmateriet/press/nyheter/lantmateriets-arbete-mot-oppna-data-i-full-gang/ */ const baseMapSource = new WMTS({ - // XXX the 'layer' parameter should be passed in the options - // dictionary (like style and version), but there is no setLayer() - // method to switch from/to the toned down map - url: 'https://minkarta.lantmateriet.se/map/topowebbcache?layer=topowebb', + url: undefined, version: '1.0.0', style: 'default', matrixSet: '3006', @@ -121,6 +118,7 @@ const view = new View({ constrainResolution: false, }); +let baseMapLayer = 'topowebb'; (function() { const params = new URLSearchParams(window.location.hash.substring(1)); const x = parseFloat(params.get('x')); @@ -132,6 +130,11 @@ const view = new View({ if (!isNaN(z)) { view.setZoom(z); } + + if (params.has('basemap')) { + baseMapLayer = params.get('basemap'); + } + baseMapSource.setUrl(`https://minkarta.lantmateriet.se/map/topowebbcache?LAYER=${encodeURIComponent(baseMapLayer)}`); })(); @@ -793,9 +796,14 @@ map.addLayer(vectorLayer); label.setAttribute('for', input.id); label.innerHTML = 'Nedtonad bakgrund karta'; + input.checked = baseMapLayer === 'topowebb_nedtonad'; input.onchange = function(event) { - const layer = event.target.checked ? 'topowebb_nedtonad' : 'topowebb'; - baseMapSource.setUrl('https://minkarta.lantmateriet.se/map/topowebbcache?LAYER=' + layer); + baseMapLayer = event.target.checked ? 'topowebb_nedtonad' : 'topowebb'; + baseMapSource.setUrl(`https://minkarta.lantmateriet.se/map/topowebbcache?LAYER=${encodeURIComponent(baseMapLayer)}`); + + const searchParams = new URLSearchParams(location.hash.substring(1)); + searchParams.set('basemap', baseMapLayer); + location.hash = '#' + searchParams.toString(); }; })(); })(); -- cgit v1.2.3