Permalink
Browse files

Merge pull request #458 from mortenbekditlevsen/reusetiles

Option for reusing tiles added to TileLayer
  • Loading branch information...
2 parents fa5adb2 + db587ab commit 3a0a28dd8f4fc569eea2a3b020ae41bce95e3366 @mourner mourner committed Feb 3, 2012
Showing with 25 additions and 3 deletions.
  1. +25 −3 src/layer/tile/TileLayer.js
@@ -17,6 +17,7 @@ L.TileLayer = L.Class.extend({
continuousWorld: false,
noWrap: false,
zoomOffset: 0,
+ reuseTiles: false,
unloadInvisibleTiles: L.Browser.mobile,
updateWhenIdle: L.Browser.mobile
@@ -125,6 +126,10 @@ L.TileLayer = L.Class.extend({
}
this._tiles = {};
+ if (this.options.reuseTiles) {
+ this._unusedTiles = [];
+ }
+
if (clearOldContainer && this._container) {
this._container.innerHTML = "";
}
@@ -145,7 +150,8 @@ L.TileLayer = L.Class.extend({
this._addTilesFromCenterOut(tileBounds);
- if (this.options.unloadInvisibleTiles) {
+ if (this.options.unloadInvisibleTiles ||
+ this.options.reuseTiles) {
this._removeOtherTiles(tileBounds);
}
},
@@ -199,6 +205,9 @@ L.TileLayer = L.Class.extend({
if (tile.parentNode === this._container) {
this._container.removeChild(tile);
}
+ if (this.options.reuseTiles) {
+ this._unusedTiles.push(this._tiles[key]);
+ }
delete this._tiles[key];
}
}
@@ -226,8 +235,8 @@ L.TileLayer = L.Class.extend({
}
}
- // create tile
- var tile = this._createTile();
+ // get unused tile - or create a new tile
+ var tile = this._getTile();
L.DomUtil.setPosition(tile, tilePos);
this._tiles[key] = tile;
@@ -271,6 +280,19 @@ L.TileLayer = L.Class.extend({
this._tileImg.style.height = tileSize + 'px';
},
+ _getTile: function () {
+ if (this.options.reuseTiles && this._unusedTiles.length > 0) {
+ var tile = this._unusedTiles.pop();
+ this._resetTile(tile);
+ return tile;
+ }
+ return this._createTile();
+ },
+
+ _resetTile: function (tile) {
+ // Override if data stored on a tile needs to be cleaned up before reuse
+ },
+
_createTile: function () {
var tile = this._tileImg.cloneNode(false);
tile.onselectstart = tile.onmousemove = L.Util.falseFn;

0 comments on commit 3a0a28d

Please sign in to comment.