Skip to content

Commit 4ce1922

Browse files
committed
remember map state
1 parent 95cad7c commit 4ce1922

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

index.html

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@
7070
<script>
7171
var map = L.map('map');
7272

73+
function saveMapState(map) {
74+
localStorage.setItem('mapState', JSON.stringify({
75+
center: map.getCenter(),
76+
zoom: map.getZoom()
77+
}));
78+
}
79+
80+
function loadMapState() {
81+
const savedState = JSON.parse(localStorage.getItem('mapState'));
82+
return savedState ? { center: [savedState.center.lat, savedState.center.lng], zoom: savedState.zoom } : null;
83+
}
84+
const savedState = loadMapState();
85+
7386
// Add a basemap (OpenStreetMap)
7487
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
7588
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
@@ -121,7 +134,7 @@
121134

122135
const coords = [];
123136
for (let j = 0; j < coordCount; j++) {
124-
// Read delta lat and lon
137+
// Read delta lon and lat
125138
const deltaLon = dataView.getInt32(offset, true) / 1000000;
126139
offset += 4;
127140
const deltaLat = dataView.getInt32(offset, true) / 1000000;
@@ -167,12 +180,18 @@
167180
bounds.extend(polyline.getBounds());
168181
});
169182

170-
if (bounds.isValid()) {
171-
map.fitBounds(bounds);
183+
if (bounds.isValid()) {
184+
if (savedState) {
185+
map.setView(savedState.center, savedState.zoom);
186+
} else {
187+
map.fitBounds(bounds);
188+
}
172189
} else {
173190
console.error('Calculated bounds are not valid.');
174191
}
175192

193+
map.on('moveend', () => saveMapState(map));
194+
map.on('zoomend', () => saveMapState(map));
176195
} catch (error) {
177196
console.error('Error rendering features:', error);
178197
}

0 commit comments

Comments
 (0)