Skip to content

Commit

Permalink
Show the last viewport when launching the page (close #86)
Browse files Browse the repository at this point in the history
  • Loading branch information
quincylvania committed Oct 7, 2024
1 parent 138028e commit a92a633
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,24 @@ window.onload = function(event) {

updateLensControl();

// default
let initialCenter = [-111.545, 39.546];
let initialZoom = 6;

// show last-open area if any (this is overriden by the URL hash map parameter)
let cachedTransformString = localStorage?.getItem('map_transform');
let cachedTransform = cachedTransformString && JSON.parse(cachedTransformString);
if (cachedTransform && cachedTransform.zoom && cachedTransform.lat && cachedTransform.lng) {
initialZoom = cachedTransform.zoom;
initialCenter = cachedTransform;
}

map = new maplibregl.Map({
container: 'map',
hash: "map",
style: './styles/basemap.json',
center: [-111.545,39.546],
zoom: 6
center: initialCenter,
zoom: initialZoom
});

// Add zoom and rotation controls to the map.
Expand Down Expand Up @@ -532,5 +544,12 @@ window.onload = function(event) {
}

map
.on('load', loadInitialMap);
.on('load', loadInitialMap)
.on('moveend', function(event) {
if (localStorage) {
let transform = map.getCenter();
transform.zoom = map.getZoom();
localStorage.setItem('map_transform', JSON.stringify(transform));
}
});
}

0 comments on commit a92a633

Please sign in to comment.