improve tile pruning

This commit is contained in:
Vladimir Agafonkin 2014-03-21 22:29:10 -07:00
parent ad0cf365e7
commit a4e8f4e1a8
2 changed files with 27 additions and 21 deletions

View File

@ -31,7 +31,7 @@
// test async // test async
setTimeout(function () { setTimeout(function () {
done(null, tile); done(null, tile);
}, Math.random() * 500); }, 500 + Math.random() * 500);
return tile; return tile;
}; };

View File

@ -35,6 +35,8 @@ L.GridLayer = L.Layer.extend({
this._update = L.Util.throttle(this._update, this.options.updateInterval, this); this._update = L.Util.throttle(this._update, this.options.updateInterval, this);
} }
this._pruneTiles = L.Util.throttle(this._pruneTiles, 200, this);
this._levels = {}; this._levels = {};
this._tiles = {}; this._tiles = {};
@ -200,8 +202,6 @@ L.GridLayer = L.Layer.extend({
_pruneTiles: function () { _pruneTiles: function () {
console.time('prune');
this._retain = {}; this._retain = {};
var bounds = this._map.getBounds(), var bounds = this._map.getBounds(),
@ -216,26 +216,26 @@ L.GridLayer = L.Layer.extend({
this._retain[key] = true; this._retain[key] = true;
if (!this._loaded[key]) { if (!this._loaded[key]) {
var found = this._retainParent(i, j, z, z - 4); var found = this._retainParent(i, j, z, z - 5);
if (!found) { if (!found) {
this._retainChildren(i, j, z, z + 1); this._retainChildren(i, j, z, z + 2);
} }
} }
} }
} }
console.timeEnd('prune'); function deferRemove(key) {
console.time('prune-remove');
for (var key in this._tiles) {
if (!this._retain[key]) { if (!this._retain[key]) {
this._removeTile(key); this._removeTile(key);
} }
} }
console.timeEnd('prune-remove'); for (var key in this._tiles) {
if (!this._retain[key]) {
setTimeout(L.bind(deferRemove, this, key), 250);
}
}
}, },
_retainParent: function (x, y, z, minZoom) { _retainParent: function (x, y, z, minZoom) {
@ -250,7 +250,7 @@ L.GridLayer = L.Layer.extend({
return true; return true;
} else if (z2 > minZoom) { } else if (z2 > minZoom) {
this._retainParent(x2, y2, z2, minZoom); return this._retainParent(x2, y2, z2, minZoom);
} }
return false; return false;
@ -463,18 +463,17 @@ L.GridLayer = L.Layer.extend({
_removeTile: function (key) { _removeTile: function (key) {
var tile = this._tiles[key]; var tile = this._tiles[key];
if (!tile) return;
if (tile) { L.DomUtil.remove(tile);
L.DomUtil.remove(tile);
delete this._tiles[key]; delete this._tiles[key];
delete this._loaded[key]; delete this._loaded[key];
this.fire('tileunload', { this.fire('tileunload', {
tile: tile, tile: tile,
coords: this._keyToTileCoords(key) coords: this._keyToTileCoords(key)
}); });
}
}, },
_initTile: function (tile) { _initTile: function (tile) {
@ -536,6 +535,13 @@ L.GridLayer = L.Layer.extend({
}); });
} }
var key = this._tileCoordsToKey(coords);
if (!this._tiles[key]) { return; }
this._loaded[key] = true;
this._pruneTiles();
L.DomUtil.addClass(tile, 'leaflet-tile-loaded'); L.DomUtil.addClass(tile, 'leaflet-tile-loaded');
this.fire('tileload', { this.fire('tileload', {