From 0936b5e18cfaedef980419f5164618305fd72e03 Mon Sep 17 00:00:00 2001 From: Mourner Date: Tue, 12 Jul 2011 20:18:22 +0300 Subject: [PATCH] map#hasLayer, ability to add tile layers below all others --- CHANGELOG.md | 2 ++ src/layer/tile/TileLayer.js | 17 ++++++++++++----- src/map/Map.js | 10 ++++++++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b89f2776..e1a0a351 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ Leaflet Changelog * Improved circles performance by not drawing them if they're off the clip region. * Improved browser-specific code to rely more on feature detection rather than user agent string. * Improved superclass access mechanism to work with inheritance chains of 3 or more classes; now you should use `Klass.superclass` instead of `this.superclass` (by [@anru](https://github.com/anru)). [#179](https://github.com/CloudMade/Leaflet/pull/179) + * Added ability to add a tile layer below all others (`map.addLayer(layer, true)`) (useful for switching base tile layers). + * Added `hasLayer` method to `Map`. ### Bugfixes diff --git a/src/layer/tile/TileLayer.js b/src/layer/tile/TileLayer.js index d69fa16c..8c2fcc1d 100644 --- a/src/layer/tile/TileLayer.js +++ b/src/layer/tile/TileLayer.js @@ -30,11 +30,11 @@ L.TileLayer = L.Class.extend({ } }, - onAdd: function(map) { + onAdd: function(map, insertAtTheTop) { this._map = map; // create a container div for tiles - this._initContainer(); + this._initContainer(insertAtTheTop); // create an image to clone for tiles this._createTileProto(); @@ -89,11 +89,18 @@ L.TileLayer = L.Class.extend({ } }, - _initContainer: function() { - var tilePane = this._map.getPanes().tilePane; + _initContainer: function(insertAtTheTop) { + var tilePane = this._map.getPanes().tilePane, + first = tilePane.firstChild; if (!this._container || tilePane.empty) { - this._container = L.DomUtil.create('div', 'leaflet-layer', tilePane); + this._container = L.DomUtil.create('div', 'leaflet-layer'); + + if (insertAtTheTop && first) { + tilePane.insertBefore(this._container, first); + } else { + tilePane.appendChild(this._container); + } this._setOpacity(this.options.opacity); } diff --git a/src/map/Map.js b/src/map/Map.js index 0c5cb501..4de88444 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -113,7 +113,7 @@ L.Map = L.Class.extend({ return this; }, - addLayer: function(layer) { + addLayer: function(layer, insertAtTheTop) { var id = L.Util.stamp(layer); if (this._layers[id]) return this; @@ -137,7 +137,7 @@ L.Map = L.Class.extend({ } var onMapLoad = function() { - layer.onAdd(this); + layer.onAdd(this, insertAtTheTop); this.fire('layeradd', {layer: layer}); }; @@ -170,6 +170,12 @@ L.Map = L.Class.extend({ return this; }, + hasLayer: function(layer) { + var id = L.Util.stamp(layer); + + return (id in this._layers); + }, + invalidateSize: function() { this._sizeChanged = true;