forked from glenrobertson/leaflet-tilelayer-geojson
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTileCache.js
More file actions
92 lines (74 loc) · 2.29 KB
/
TileCache.js
File metadata and controls
92 lines (74 loc) · 2.29 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Simple tile cache to keep tiles while zooming with overzoom
*/
L.TileCache = function() {
};
L.TileCache.prototype = {
// cache key: tile (String: Object)
_cache: {},
// flag to determine switch between tile unloading (put) and loading (get) phase
_unloading: false,
// flag to only cache tiles when zooming, not when moving
_zooming: false,
onAdd: function(map) {
this._map = map;
map.on('zoomstart', this._onZoomStart, this);
map.on('zoomend', this._onZoomEnd, this);
},
onRemove: function(map) {
this._map = null;
map.off('zoomstart', this._onZoomStart, this);
map.off('zoomend', this._onZoomEnd, this);
},
_onZoomStart: function(evt) {
this._zooming = true;
},
_onZoomEnd: function(evt) {
this._zooming = false;
},
get: function(key, urlZoom) {
var ckey = this._getCacheKey(key, urlZoom);
var tile = this._cache[ckey];
this._unloading = false;
//console.log('cache ' + (tile ? 'hit ' : 'miss') + ': ' + ckey);
return tile;
},
put: function(tile) {
if (!this._zooming) return;
if (!this._unloading) {
// clear old entries before adding newly removed tiles after zoom or move
this.clear();
this._unloading = true;
}
var ckey = this._getCacheKeyFromTile(tile);
if (!(ckey in this._cache)) {
// vector layer is recreated because of feature filter
delete tile.layer;
this._cache[ckey] = tile;
//console.log('cache put : ' + ckey + ' (' + Object.keys(this._cache).length + ')');
}
},
clear: function() {
//console.log('cache clear');
this._cache = {};
},
_getCacheKeyFromTile: function(tile) {
return this._getCacheKey(tile.key, tile.urlZoom);
},
_getCacheKey: function(key, urlZoom) {
return urlZoom + ':' + key
}
};
L.tileCache = function() {
return new L.TileCache();
};
// dummy impl. to turn caching off
L.tileCacheNone = function() {
return {
onAdd: function(map) {},
onRemove: function(map) {},
get: function(key, urlZoom) {},
put: function(tile) {},
clear: function() {}
};
};