From 5eab3d46e523b28799d2201cc40890bd192c1ddb Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Thu, 28 Dec 2023 11:16:05 +0100 Subject: [PATCH] Honour coords.z when passed to TileLayer.getTileUrl cf #6004 #6006 Before this change, `coords.z` would not taken into account, and was always overriden by `this._tileZoom`. In the normal flow, `coords.z` is anyway set from `this._tileZoom` before `getTileUrl` is called (in GridLayer._update). But this prevents using `getTileUrl` out of this `_update` method. There are some scenarios where this is needed (see #5822), or (which is the case in uMap) when wanting to display a tile as a tilelayer preview. --- spec/suites/layer/tile/TileLayerSpec.js | 12 ++++++++++++ src/layer/tile/TileLayer.js | 5 ++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/spec/suites/layer/tile/TileLayerSpec.js b/spec/suites/layer/tile/TileLayerSpec.js index 867777428d9..ba2a22bc18c 100644 --- a/spec/suites/layer/tile/TileLayerSpec.js +++ b/spec/suites/layer/tile/TileLayerSpec.js @@ -509,4 +509,16 @@ describe('TileLayer', () => { layer.setUrl(placeKitten); }); }); + + describe('#getTileUrl', function () { + it('can call with static coords', function () { + // Layer is not added to the map + var layer = L.tileLayer('http://example.com/{z}/{x}/{y}.png'); + var url = layer.getTileUrl({z: 1, x: 2, y: 3}); + expect(url).to.equal('http://example.com/1/2/3.png'); + layer.addTo(map) + var url = layer.getTileUrl({z: 1, x: 2, y: 3}); + expect(url).to.equal('http://example.com/1/2/3.png'); + }); + }); }); diff --git a/src/layer/tile/TileLayer.js b/src/layer/tile/TileLayer.js index 44b347b302d..6f62b7c311f 100644 --- a/src/layer/tile/TileLayer.js +++ b/src/layer/tile/TileLayer.js @@ -181,7 +181,7 @@ export const TileLayer = GridLayer.extend({ s: this._getSubdomain(coords), x: coords.x, y: coords.y, - z: this._getZoomForUrl() + z: this._getZoomForUrl(coords.z) }; if (this._map && !this._map.options.crs.infinite) { const invertedY = this._globalTileRange.max.y - coords.y; @@ -210,8 +210,7 @@ export const TileLayer = GridLayer.extend({ e.tile.onload = null; }, - _getZoomForUrl() { - let zoom = this._tileZoom; + _getZoomForUrl: function (zoom) { const maxZoom = this.options.maxZoom, zoomReverse = this.options.zoomReverse, zoomOffset = this.options.zoomOffset;