Skip to content

Commit 91351a9

Browse files
committed
Implement map hash
1 parent 77ad9a7 commit 91351a9

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/lib/components/data-vis/map/Map.svelte

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
quantileBreaks,
2626
} from "./mapUtils.js";
2727
import NonStandardControls from "./NonStandardControls.svelte";
28+
import { replaceState } from "$app/navigation";
2829
2930
import { joinData } from "./dataJoin";
31+
import { page } from "$app/state";
3032
3133
let {
3234
data,
@@ -57,6 +59,10 @@
5759
hoverOpacity = 0.8,
5860
center = [-2.5, 53],
5961
zoom = 5,
62+
hash = false,
63+
updateHash = (u) => {
64+
replaceState(u, page.state);
65+
},
6066
mapHeight = 200,
6167
} = $props();
6268
@@ -188,6 +194,9 @@
188194
]);
189195
}
190196
}
197+
//Even if hash is false, if the page is loaded with a location hash use that as the initial settings, rather than than the values passed to the component
198+
const initialLocationHash = page.url.hash.replace("#", "").split("/");
199+
const useLocationHash = initialLocationHash.length >= 3 ? true : false;
191200
</script>
192201

193202
<div style="height: {mapHeight}px;">
@@ -196,8 +205,12 @@
196205
bind:loaded
197206
{style}
198207
{standardControls}
199-
{center}
200-
{zoom}
208+
center={useLocationHash
209+
? [initialLocationHash[2], initialLocationHash[1]]
210+
: center}
211+
zoom={useLocationHash ? initialLocationHash[0] : zoom}
212+
{hash}
213+
{updateHash}
201214
class="map"
202215
>
203216
{#if !standardControls}

src/wrappers/components/data-vis/map/MapWrapper.svelte

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,14 @@
489489
value: 5,
490490
description: "The initial zoom level",
491491
},
492+
{
493+
name: "hash",
494+
isProp: true,
495+
description:
496+
"Set to true to track the map viewport in the URL hash. If the URL hash is set, that overrides initial viewport settings.",
497+
category: "View",
498+
value: false,
499+
},
492500
]),
493501
);
494502

0 commit comments

Comments
 (0)