-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL.LastView.js
76 lines (64 loc) · 2.18 KB
/
L.LastView.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
L.LastView = L.Class.extend({
options: {
lat:52.2,
lon:10.5,
zoom:10
},
_haslocalStorage: false,
initialize: function (options) {
L.Util.setOptions(this, options);
},
addTo: function (map) {
this.onAdd(map);
},
onAdd: function (map) {
this._map = map;
map.on('moveend', this._updateLastPosition, this);
if(typeof(Storage)!=="undefined")
{ //supported it
this._haslocalstorage = true;
}else{
this._haslocalstorage = false;
//use only online storage or cookies
console.warn("Localstorage is not supported that means storing data localy is not possible");
}
if ((this._haslocalstorage) && (localStorage.LastPositionLat != undefined)) {
console.log("restore old position");
this._map.setView(new L.LatLng(
localStorage.getItem("LastPositionLat"),
localStorage.getItem("LastPositionLon")),
localStorage.getItem("LastPositionZoom"));
}else if (this._haslocalstorage == false) {
// TODO: save position in cookies
}else{
console.log("use default position");
console.log(this.options);
this._map.setView(new L.LatLng(this.options.lat, this.options.lon), this.options.zoom);
//this._map.setView(new L.LatLng(52.2, 10.2), 10);
}
//return container;
},
onRemove: function (map) {
console.log("remove layer");
map.off({
'moveend': this._updateLastPosition
}, this);
this._map = null;
},
_updateLastPosition: function (event) {
if (this._haslocalstorage) {
var zoom = this._map.getZoom();
//console.log(this._map.getCenter());
var lat = this._map.getCenter().lat;
var lon = this._map.getCenter().lng;
localStorage.setItem("LastPositionLat", lat);
localStorage.setItem("LastPositionLon", lon);
localStorage.setItem("LastPositionZoom", zoom);
}else{
//TODO restore from cookie
}
}
});
L.lastView = function(options) {
return new L.LastView(options);
}