TileLayer rounds fractional zoom in url to integer {z} (#8613)

Co-authored-by: Florian Bischof <design.falke@gmail.com>
This commit is contained in:
Ray Chan
2025-03-02 04:54:26 +08:00
committed by GitHub
parent 145a7a14da
commit 511f8f11e7
2 changed files with 27 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import {expect} from 'chai';
import {Browser, CRS, DomUtil, Map, TileLayer, Util} from 'leaflet';
import {Browser, CRS, DomUtil, Map, TileLayer, Util, LatLng} from 'leaflet';
import sinon from 'sinon';
import {createContainer, removeMapContainer} from '../../SpecHelper.js';
@ -418,6 +418,26 @@ describe('TileLayer', () => {
expect(layer.options.attribution).to.eql('');
});
it('requests tiles with an integer {z} when the map\'s zoom level is fractional', () => {
const layer = new TileLayer('http://example.com/{z}/{y}/{x}.png').addTo(map);
map.options.zoomSnap = 0;
map._resetView(new LatLng(0, 0), 2.3);
layer.redraw();
const urls = [
'http://example.com/2/1/1.png',
'http://example.com/2/1/2.png',
'http://example.com/2/2/1.png',
'http://example.com/2/2/2.png',
];
let i = 0;
eachImg(layer, (img) => {
expect(img.src).to.eql(urls[i]);
i++;
});
});
});
const _describe = 'crossOrigin' in DomUtil.create('img') ? describe : describe.skip; // skip in IE<11

View File

@ -224,8 +224,8 @@ export const TileLayer = GridLayer.extend({
_getZoomForUrl() {
let zoom = this._tileZoom;
const maxZoom = this.options.maxZoom,
zoomReverse = this.options.zoomReverse,
zoomOffset = this.options.zoomOffset;
zoomReverse = this.options.zoomReverse,
zoomOffset = this.options.zoomOffset;
if (zoomReverse) {
zoom = maxZoom - zoom;
@ -281,6 +281,10 @@ export const TileLayer = GridLayer.extend({
}
return GridLayer.prototype._tileReady.call(this, coords, err, tile);
},
_clampZoom(zoom) {
return Math.round(GridLayer.prototype._clampZoom.call(this, zoom));
}
});