Merge pull request #5088 from bb-juliogarcia/minNativeZoom

This commit is contained in:
Per Liedman 2016-11-18 15:46:22 +01:00
commit 54d6fc24c1
2 changed files with 128 additions and 14 deletions

View File

@ -378,4 +378,93 @@ describe('TileLayer', function () {
});
});
});
describe('minNativeZoom', function () {
before(function () {
div = document.createElement('div');
div.style.width = '400px';
div.style.height = '400px';
div.style.visibility = 'hidden';
document.body.appendChild(div);
map = L.map(div).setView([0, 0], 0);
});
it('returns downscaled tileSize when zoom is lower than minNativeZoom (zoom = 0, minNativeZoom = 1)', function () {
var layer = L.tileLayer('http://{s}.example.com/{z}/{-y}/{x}.png', {
minNativeZoom: 1,
tileSize: 256
}).addTo(map);
map.setView([0, 0], 0);
expect(map.getZoom()).to.equal(0);
expect(layer.getTileSize().x).to.equal(128);
expect(layer.getTileSize().y).to.equal(128);
});
it('returns regular tileSize when zoom is equal to minNativeZoom (zoom = 1, minNativeZoom = 1)', function () {
var layer = L.tileLayer('http://{s}.example.com/{z}/{-y}/{x}.png', {
minNativeZoom: 1,
tileSize: 256
}).addTo(map);
map.setView([0, 0], 1);
expect(map.getZoom()).to.equal(1);
expect(layer.getTileSize().x).to.equal(256);
expect(layer.getTileSize().y).to.equal(256);
});
it('returns regular tileSize when zoom is greater than minNativeZoom (zoom = 2, minNativeZoom = 1)', function () {
var layer = L.tileLayer('http://{s}.example.com/{z}/{-y}/{x}.png', {
minNativeZoom: 1,
tileSize: 256
}).addTo(map);
map.setView([0, 0], 2);
expect(map.getZoom()).to.equal(2);
expect(layer.getTileSize().x).to.equal(256);
expect(layer.getTileSize().y).to.equal(256);
});
});
describe('maxNativeZoom', function () {
before(function () {
div = document.createElement('div');
div.style.width = '400px';
div.style.height = '400px';
div.style.visibility = 'hidden';
document.body.appendChild(div);
map = L.map(div).setView([0, 0], 0);
});
it('returns upscaled tileSize when zoom is higher than maxNativeZoom (zoom = 2, maxNativeZoom = 1)', function () {
var layer = L.tileLayer('http://{s}.example.com/{z}/{-y}/{x}.png', {
maxNativeZoom: 1,
tileSize: 256
}).addTo(map);
map.setView([0, 0], 2);
expect(layer.getTileSize().x).to.equal(512);
expect(layer.getTileSize().y).to.equal(512);
});
it('returns regular tileSize when zoom is equal to minNativeZoom (zoom = 1, maxNativeZoom = 1)', function () {
var layer = L.tileLayer('http://{s}.example.com/{z}/{-y}/{x}.png', {
maxNativeZoom: 1,
tileSize: 256
}).addTo(map);
map.setView([0, 0], 1);
expect(layer.getTileSize().x).to.equal(256);
expect(layer.getTileSize().y).to.equal(256);
});
it('returns regular tileSize when zoom is lower than minNativeZoom (zoom = 0, maxNativeZoom = 1)', function () {
var layer = L.tileLayer('http://{s}.example.com/{z}/{-y}/{x}.png', {
maxNativeZoom: 1,
tileSize: 256
}).addTo(map);
map.setView([0, 0], 0);
expect(layer.getTileSize().x).to.equal(256);
expect(layer.getTileSize().y).to.equal(256);
});
});
});

View File

@ -48,6 +48,12 @@ L.TileLayer = L.GridLayer.extend({
// from `maxNativeZoom` level and auto-scaled.
maxNativeZoom: null,
// @option minNativeZoom: Number = null
// Minimum zoom number the tile source has available. If it is specified,
// the tiles on all zoom levels lower than `minNativeZoom` will be loaded
// from `minNativeZoom` level and auto-scaled.
minNativeZoom: null,
// @option subdomains: String|String[] = 'abc'
// Subdomains of the tile service. Can be passed in the form of one string (where each letter is a subdomain name) or an array of strings.
subdomains: 'abc',
@ -195,14 +201,22 @@ L.TileLayer = L.GridLayer.extend({
getTileSize: function () {
var map = this._map,
tileSize = L.GridLayer.prototype.getTileSize.call(this),
zoom = this._tileZoom + this.options.zoomOffset,
zoomN = this.options.maxNativeZoom;
tileSize = L.GridLayer.prototype.getTileSize.call(this),
zoom = this._tileZoom + this.options.zoomOffset,
minNativeZoom = this.options.minNativeZoom,
maxNativeZoom = this.options.maxNativeZoom;
// increase tile size when overscaling
return zoomN !== null && zoom > zoomN ?
tileSize.divideBy(map.getZoomScale(zoomN, zoom)).round() :
tileSize;
// decrease tile size when scaling below minNativeZoom
if (minNativeZoom !== null && zoom < minNativeZoom) {
return tileSize.divideBy(map.getZoomScale(minNativeZoom, zoom)).round();
}
// increase tile size when scaling above maxNativeZoom
if (maxNativeZoom !== null && zoom > maxNativeZoom) {
return tileSize.divideBy(map.getZoomScale(maxNativeZoom, zoom)).round();
}
return tileSize;
},
_onTileRemove: function (e) {
@ -210,17 +224,28 @@ L.TileLayer = L.GridLayer.extend({
},
_getZoomForUrl: function () {
var zoom = this._tileZoom,
maxZoom = this.options.maxZoom,
zoomReverse = this.options.zoomReverse,
zoomOffset = this.options.zoomOffset,
minNativeZoom = this.options.minNativeZoom,
maxNativeZoom = this.options.maxNativeZoom;
var options = this.options,
zoom = this._tileZoom;
if (options.zoomReverse) {
zoom = options.maxZoom - zoom;
if (zoomReverse) {
zoom = maxZoom - zoom;
}
zoom += options.zoomOffset;
zoom += zoomOffset;
return options.maxNativeZoom !== null ? Math.min(zoom, options.maxNativeZoom) : zoom;
if (minNativeZoom !== null && zoom < minNativeZoom) {
return minNativeZoom;
}
if (maxNativeZoom !== null && zoom > maxNativeZoom) {
return maxNativeZoom;
}
return zoom;
},
_getSubdomain: function (tilePoint) {