Skip to content

Commit

Permalink
Merge pull request #8 from willtonkin/add-layers-to-url-hash
Browse files Browse the repository at this point in the history
Add selected layers state to URL hash
  • Loading branch information
TimGremalm authored Sep 16, 2023
2 parents 03ccac2 + d54aee4 commit 185848e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/loaders/loadGeoJsonFeatureCollections.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const loadGeoJsonFeatureCollections = async (map, styleFunction, groupByP
const geojsonLayer = L.geoJSON(geojsonData, {
filter: filterByProperty(groupByProperty, value),
style: styleFunction(value),
}).addTo(map);
});

map.groups[value] = geojsonLayer;
});
Expand Down
60 changes: 55 additions & 5 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,36 @@ 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']);

const map = L.map('map', { zoomControl: false, maxZoom: 21, drawControl: true, attributionControl: false })
.setView([57.6226, 14.9276], 16);

map.groups = {};

map.groups.googleSatellite = L.tileLayer('https://{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}', {
Expand All @@ -35,8 +61,6 @@ export const createMap = async () => {
showBetaMsg();
}

new L.Hash(map); // Makes the URL follow the map.

map.groups.mapstuff = new L.LayerGroup();

//Load placenames
Expand Down Expand Up @@ -79,7 +103,6 @@ export const createMap = async () => {
map.groups.parking.addTo(map.groups.mapstuff);
map.groups.toilet.addTo(map.groups.mapstuff);
map.groups.bridge.addTo(map.groups.mapstuff);
map.groups.mapstuff.addTo(map);


//Initialize the editor (it loads it data at the end)
Expand Down Expand Up @@ -145,7 +168,22 @@ export const createMap = async () => {
Check_Clean: map.groups.clean,
};

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

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

visibleLayers.forEach(layer => map.addLayer(extraLayers[layer]))
});

const hash = new L.Hash(map); // Makes the URL follow the map.

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

map.on('overlayadd', function (eventLayer)
{
if (eventLayer.name === 'Check_Power') editor.setLayerFilter('power', false);
else if (eventLayer.name === 'Check_Sound') editor.setLayerFilter('sound', false);
Expand All @@ -170,6 +208,18 @@ export const createMap = async () => {
console.log(e.latlng);
});

// 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
L.control.layers(undefined, extraLayers).addTo(map);

Expand Down

0 comments on commit 185848e

Please sign in to comment.