Merge pull request #1159 from moonlite/master

First stab at a fix for Issue 623
This commit is contained in:
Vladimir Agafonkin 2012-11-29 06:44:25 -08:00
commit 0c5f401220
5 changed files with 157 additions and 10 deletions

45
debug/map/zoomlevels.html Normal file
View File

@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<title>Leaflet debug page</title>
<link rel="stylesheet" href="../../dist/leaflet.css" />
<!--[if lte IE 8]><link rel="stylesheet" href="../../dist/leaflet.ie.css" /><![endif]-->
<link rel="stylesheet" href="../css/screen.css" />
<script src="../leaflet-include.js"></script>
</head>
<body>
<div id="map"></div>
<script type="text/javascript">
// Test that changing between layers with differing zoomlevels also updates
// the zoomlevels in the map + also
function getCloudMadeUrl(styleId) {
return 'http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/' + styleId + '/256/{z}/{x}/{y}.png';
}
var map = L.map('map').setView(L.latLng(50.5, 30.51), 0);
var cloudmadeAttribution = 'Map data &copy; 2011 OpenStreetMap contributors, Imagery &copy; 2011 CloudMade',
cloudmade = L.tileLayer(getCloudMadeUrl(997), {attribution: cloudmadeAttribution, minZoom: 0, maxZoom: 10}).addTo(map),
cloudmade2 = L.tileLayer(getCloudMadeUrl(998), {attribution: 'Hello world', minZoom: 5, maxZoom: 18});
L.control.layers({
'CloudMade Pale Dawn (5-18)': cloudmade2,
'CloudMade Fresh (0-10)': cloudmade
}).addTo(map);
L.control.scale().addTo(map);
for (var i = 0; i < 1000; i++) {
L.marker(getRandomLatLng(map)).addTo(map);
}
</script>
</body>
</html>

View File

@ -11,11 +11,13 @@ describe("Control.Layers", function () {
layers = L.control.layers(baseLayers).addTo(map), layers = L.control.layers(baseLayers).addTo(map),
spy = jasmine.createSpy(); spy = jasmine.createSpy();
map.on('baselayerchange', spy); map.on('baselayerchange', spy)
happen.click(layers._baseLayersList.getElementsByTagName("input")[0]); .whenReady(function(){
happen.click(layers._baseLayersList.getElementsByTagName("input")[0]);
expect(spy).toHaveBeenCalled(); expect(spy).toHaveBeenCalled();
expect(spy.mostRecentCall.args[0].layer).toBe(baseLayers["Layer 1"]); expect(spy.mostRecentCall.args[0].layer).toBe(baseLayers["Layer 1"]);
});
}); });
it("is not fired on input that doesn't change the base layer", function () { it("is not fired on input that doesn't change the base layer", function () {

View File

@ -1 +1,75 @@
describe('TileLayer', noSpecs);
describe('TileLayer', function () {
describe("#getMaxZoom, #getMinZoom", function () {
var map;
beforeEach(function () {
map = L.map(document.createElement('div'));
});
describe("when a tilelayer is added to a map with no other layers", function () {
it("should have the same zoomlevels as the tilelayer", function () {
var maxZoom = 10,
minZoom = 5;
map.setView([0, 0], 1);
L.tileLayer("{z}{x}{y}", {
maxZoom: maxZoom,
minZoom: minZoom
}).addTo(map);
expect(map.getMaxZoom() === maxZoom).toBeTruthy();
expect(map.getMinZoom() === minZoom).toBeTruthy();
});
});
describe("when a tilelayer is added to a map that already has a tilelayer", function () {
it("should have its zoomlevels updated to fit the new layer", function () {
map.setView([0, 0], 1);
L.tileLayer("{z}{x}{y}", { minZoom:10, maxZoom: 15 }).addTo(map);
expect(map.getMinZoom()).toBe(10);
expect(map.getMaxZoom()).toBe(15);
L.tileLayer("{z}{x}{y}", { minZoom:5, maxZoom: 10 }).addTo(map);
expect(map.getMinZoom()).toBe(5); // changed
expect(map.getMaxZoom()).toBe(15); // unchanged
L.tileLayer("{z}{x}{y}",{ minZoom:10, maxZoom: 20 }).addTo(map);
expect(map.getMinZoom()).toBe(5); // unchanged
expect(map.getMaxZoom()).toBe(20); // changed
L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 25 }).addTo(map);
expect(map.getMinZoom()).toBe(0); // changed
expect(map.getMaxZoom()).toBe(25); // changed
});
});
describe("when a tilelayer is removed from a map", function () {
it("it should have its zoomlevels updated to only fit the layers it currently has", function () {
var tiles = [ L.tileLayer("{z}{x}{y}", { minZoom:10, maxZoom: 15 }).addTo(map),
L.tileLayer("{z}{x}{y}", { minZoom:5, maxZoom: 10 }).addTo(map),
L.tileLayer("{z}{x}{y}", { minZoom:10, maxZoom: 20 }).addTo(map),
L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 25 }).addTo(map)
];
map.whenReady(function() {
expect(map.getMinZoom()).toBe(0);
expect(map.getMaxZoom()).toBe(25);
map.removeLayer(tiles[0]);
expect(map.getMinZoom()).toBe(0);
expect(map.getMaxZoom()).toBe(25);
map.removeLayer(tiles[3]);
expect(map.getMinZoom()).toBe(5);
expect(map.getMaxZoom()).toBe(20);
map.removeLayer(tiles[2]);
expect(map.getMinZoom()).toBe(5);
expect(map.getMaxZoom()).toBe(10);
map.removeLayer(tiles[1]);
expect(map.getMinZoom()).toBe(0);
expect(map.getMaxZoom()).toBe(Infinity);
});
});
});
});
});

View File

@ -220,6 +220,7 @@ L.Control.Layers = L.Control.extend({
} }
if (baseLayer) { if (baseLayer) {
this._map.setZoom(this._map.getZoom());
this._map.fire('baselayerchange', {layer: baseLayer}); this._map.fire('baselayerchange', {layer: baseLayer});
} }

View File

@ -148,11 +148,9 @@ L.Map = L.Class.extend({
this._layers[id] = layer; this._layers[id] = layer;
// TODO getMaxZoom, getMinZoom in ILayer (instead of options) // TODO getMaxZoom, getMinZoom in ILayer (instead of options)
if (layer.options && !isNaN(layer.options.maxZoom)) { if(layer.options && (!isNaN(layer.options.maxZoom) || !isNaN(layer.options.minZoom))){
this._layersMaxZoom = Math.max(this._layersMaxZoom || 0, layer.options.maxZoom); this._zoomBoundLayers[id] = layer;
} this._updateZoomLevels();
if (layer.options && !isNaN(layer.options.minZoom)) {
this._layersMinZoom = Math.min(this._layersMinZoom || Infinity, layer.options.minZoom);
} }
// TODO looks ugly, refactor!!! // TODO looks ugly, refactor!!!
@ -178,6 +176,10 @@ L.Map = L.Class.extend({
layer.onRemove(this); layer.onRemove(this);
delete this._layers[id]; delete this._layers[id];
if(this._zoomBoundLayers[id]){
delete this._zoomBoundLayers[id];
this._updateZoomLevels();
}
// TODO looks ugly, refactor // TODO looks ugly, refactor
if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) { if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) {
@ -477,6 +479,7 @@ L.Map = L.Class.extend({
layers = layers ? (layers instanceof Array ? layers : [layers]) : []; layers = layers ? (layers instanceof Array ? layers : [layers]) : [];
this._layers = {}; this._layers = {};
this._zoomBoundLayers = {};
this._tileLayersNum = 0; this._tileLayersNum = 0;
var i, len; var i, len;
@ -535,6 +538,28 @@ L.Map = L.Class.extend({
L.DomUtil.setPosition(this._mapPane, this._getMapPanePos().subtract(offset)); L.DomUtil.setPosition(this._mapPane, this._getMapPanePos().subtract(offset));
}, },
_updateZoomLevels: function () {
var i,
minZoom = Infinity,
maxZoom = -Infinity;
for (i in this._zoomBoundLayers) {
var layer = this._zoomBoundLayers[i];
if(!isNaN(layer.options.minZoom)){
minZoom = Math.min(minZoom, layer.options.minZoom);
}
if(!isNaN(layer.options.maxZoom)){
maxZoom = Math.max(maxZoom, layer.options.maxZoom);
}
}
if(i === undefined){ // we have no tilelayers
this._layersMaxZoom = this._layersMinZoom = undefined;
} else {
this._layersMaxZoom = maxZoom;
this._layersMinZoom = minZoom;
}
},
// map events // map events