-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathleaflet.permalink.js
More file actions
56 lines (51 loc) · 1.96 KB
/
Copy pathleaflet.permalink.js
File metadata and controls
56 lines (51 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
L.Permalink = {
//gets the map center, zoom-level and rotation from the URL if present, else uses default values
getMapLocation: function (zoom, center) {
'use strict';
zoom = (zoom || zoom === 0) ? zoom : 13;
center = (center) ? center : [50.52, 10.87];
if (window.location.hash !== '') {
var hash = window.location.hash.replace('#', '');
var parts = hash.split(',');
if (parts.length === 3) {
center = {
lat: parseFloat(parts[0]),
lng: parseFloat(parts[1])
};
zoom = parseInt(parts[2].slice(0, -1), 10);
}
}
return {zoom: zoom, center: center};
},
setup: function (map) {
'use strict';
var shouldUpdate = true;
var updatePermalink = function () {
if (!shouldUpdate) {
// do not update the URL when the view was changed in the 'popstate' handler (browser history navigation)
shouldUpdate = true;
return;
}
var center = map.getCenter();
var hash = '#' +
Math.round(center.lat * 100000) / 100000 + ',' +
Math.round(center.lng * 100000) / 100000 + ',' +
map.getZoom() + 'z';
var state = {
zoom: map.getZoom(),
center: center
};
window.history.pushState(state, 'map', hash);
};
map.on('moveend', updatePermalink);
// restore the view state when navigating through the history, see
// https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate
window.addEventListener('popstate', function (event) {
if (event.state === null) {
return;
}
map.setView(event.state.center, event.state.zoom);
shouldUpdate = false;
});
}
};