-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathleaflet.restoreview.js
50 lines (46 loc) · 1.47 KB
/
leaflet.restoreview.js
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
(function() {
var RestoreViewMixin = {
restoreView: function () {
if (!storageAvailable('localStorage')) {
return false;
}
var storage = window.localStorage;
if (!this.__initRestore) {
this.on('moveend', function (e) {
if (!this._loaded)
return; // Never access map bounds if view is not set.
var view = {
lat: this.getCenter().lat,
lng: this.getCenter().lng,
zoom: this.getZoom()
};
storage['mapView'] = JSON.stringify(view);
}, this);
this.__initRestore = true;
}
var view = storage['mapView'];
try {
view = JSON.parse(view || '');
this.setView(L.latLng(view.lat, view.lng), view.zoom, true);
return true;
}
catch (err) {
return false;
}
}
};
function storageAvailable(type) {
try {
var storage = window[type],
x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch(e) {
console.warn("Your browser blocks access to " + type);
return false;
}
}
L.Map.include(RestoreViewMixin);
})();