Skip to content

Commit

Permalink
change url hash schema to the form .../layers:1,2,3/
Browse files Browse the repository at this point in the history
  • Loading branch information
willtonkin committed Aug 23, 2023
1 parent c4a9315 commit d54aee4
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@ import { addLegends } from './loaders/addLegends';

import { Editor } from './editor';

function encodeHashMeta(layers) {
if (layers instanceof Set) {
layers = Array.from(layers);
}

const hashMeta = [];

if (layers.length > 0) {
hashMeta.push('layers:' + layers.join(','))
}

return hashMeta;
}

function decodeHashMeta(hashMeta) {
const layersKey = 'layers:';
const layersVal = hashMeta.find(val => val.startsWith(layersKey));

const layers = layersVal ? layersVal.substring(layersKey.length).split(',') : [];

return { layers };
}

export const createMap = async () => {
// default visible map layers
let visibleLayers = new Set(['Placement', 'Placement_map']);
Expand Down Expand Up @@ -146,7 +169,9 @@ export const createMap = async () => {
};

map.on("hashmetainit", function(initState) {
initState.meta
const { layers } = decodeHashMeta(initState.meta);

layers
.filter(name => name in extraLayers)
.forEach(layerName => visibleLayers.add(layerName));

Expand All @@ -156,7 +181,7 @@ export const createMap = async () => {
const hash = new L.Hash(map); // Makes the URL follow the map.

// force update of URL hash on first load
hash.setHashMeta(Array.from(visibleLayers), true);
hash.setHashMeta(encodeHashMeta(visibleLayers), true);

map.on('overlayadd', function (eventLayer)
{
Expand All @@ -183,15 +208,16 @@ export const createMap = async () => {
console.log(e.latlng);
});

map.on('overlayadd overlayremove', function (event) {
if (event.type === 'overlayadd') {
visibleLayers.push(event.name);
}
else if (event.type === 'overlayremove') {
visibleLayers.splice(visibleLayers.indexOf(event.name), 1);
}

hash.setHashMeta(visibleLayers, true);
// maintain visible layers list and update hash
map.on('overlayadd', function (event) {
visibleLayers.add(event.name);
// update hash state
hash.setHashMeta(encodeHashMeta(visibleLayers), true);
});
map.on('overlayremove', function (event) {
visibleLayers.delete(event.name);
// update hash state
hash.setHashMeta(encodeHashMeta(visibleLayers), true);
});

// Add layer control and legends
Expand Down

0 comments on commit d54aee4

Please sign in to comment.