Skip to content

Commit cf766e6

Browse files
author
Peter Rushforth
committed
Fix tile clean up in MapTileLayer esp for CBMTILE <map-tile>s
1 parent ef15644 commit cf766e6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/mapml/layers/MapTileLayer.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ export var MapTileLayer = GridLayer.extend({
3737
// this.zoomBounds = this._computeZoomBounds();
3838
},
3939

40+
getEvents: function () {
41+
const events = GridLayer.prototype.getEvents
42+
? GridLayer.prototype.getEvents.call(this)
43+
: {};
44+
45+
// Add our custom zoom change handler
46+
events.zoomend = this._handleZoomChange;
47+
48+
return events;
49+
},
4050
onAdd: function (map) {
4151
this.options.pane.appendChild(this._container);
4252
// Call the parent method
@@ -46,8 +56,25 @@ export var MapTileLayer = GridLayer.extend({
4656
// Clean up pending tiles
4757
this._pendingTiles = {};
4858
DomUtil.remove(this._container);
59+
GridLayer.prototype.onRemove.call(this, map);
4960
},
5061

62+
_handleZoomChange: function () {
63+
// this is necessary for CBMTILE in particular, I think because
64+
// Leaflet relies on Web Mercator powers of 2 inter-zoom tile relations
65+
// to calculate what tiles to clean up/remove. CBMTILE doesn't have that
66+
// relationship between zoom levels /tiles.
67+
//
68+
// Force removal of all tiles that don't match current zoom
69+
const currentZoom = this._map.getZoom();
70+
71+
for (const key in this._tiles) {
72+
const coords = this._keyToTileCoords(key);
73+
if (coords.z !== currentZoom) {
74+
this._removeTile(key);
75+
}
76+
}
77+
},
5178
/**
5279
* Adds a map-tile element to the layer
5380
* @param {HTMLTileElement} mapTile - The map-tile element to add

0 commit comments

Comments
 (0)