diff --git a/js/Map.js b/js/Map.js index 638214a..6a992c1 100644 --- a/js/Map.js +++ b/js/Map.js @@ -63,11 +63,6 @@ BR.Map = { overlays[i] = L.tileLayer(BR.conf.overlays[i]); } } - // after applying custom base layer configurations, add configured base layer to map - var defaultLayer = baseLayers[Object.keys(baseLayers)[BR.conf.defaultBaseLayerIndex || 0]]; - if (defaultLayer) { - map.addLayer(defaultLayer); - } layersControl = BR.layersTab(layersConfig, baseLayers, overlays).addTo(map); diff --git a/js/control/Control.Layers.js b/js/control/Control.Layers.js index 17fd550..bcff8f0 100644 --- a/js/control/Control.Layers.js +++ b/js/control/Control.Layers.js @@ -17,6 +17,18 @@ BR.ControlLayers = L.Control.Layers.extend({ return result; }, + getActiveBaseLayer: function () { + var activeLayers = this.getActiveLayers(); + for (var i = 0; i < activeLayers.length; i++) { + var obj = activeLayers[i]; + if (!obj.overlay) { + return obj; + } + } + + return null; + }, + removeActiveLayers: function () { var removed = []; @@ -42,6 +54,12 @@ BR.ControlLayers = L.Control.Layers.extend({ return null; }, + getBaseLayers: function () { + return this._layers.filter(function (obj) { + return !obj.overlay; + }); + }, + activateLayer: function (layer) { this._map.addLayer(layer); }, @@ -54,6 +72,13 @@ BR.ControlLayers = L.Control.Layers.extend({ break; } } + }, + + activateBaseLayerIndex: function (index) { + var baseLayers = this.getBaseLayers(); + var obj = baseLayers[index]; + + this.activateLayer(obj.layer); } }); \ No newline at end of file diff --git a/js/control/LayersTab.js b/js/control/LayersTab.js index 6e5ced2..1798035 100644 --- a/js/control/LayersTab.js +++ b/js/control/LayersTab.js @@ -315,6 +315,14 @@ BR.LayersTab = BR.ControlLayers.extend({ return obj; }, + activateDefaultBaseLayer: function () { + var index = BR.conf.defaultBaseLayerIndex || 0; + var activeBaseLayer = this.getActiveBaseLayer(); + if (!activeBaseLayer) { + this.activateBaseLayerIndex(index); + } + }, + saveRemoveActiveLayers: function () { this.saveLayers = this.removeActiveLayers(); }, diff --git a/js/index.js b/js/index.js index e55f5a5..c00b916 100644 --- a/js/index.js +++ b/js/index.js @@ -314,6 +314,12 @@ layersControl: layersControl }); + // activate configured default base layer or first if no hash, + // only after hash init, by using the same delay + setTimeout(function () { + layersControl.activateDefaultBaseLayer(); + }, urlHash.changeDefer); + routingOptions.on('update', urlHash.onMapMove, urlHash); nogos.on('update', urlHash.onMapMove, urlHash); // waypoint add, move, delete (but last) diff --git a/js/plugin/leaflet-fullHash.js b/js/plugin/leaflet-fullHash.js index 2802da9..f148e9e 100644 --- a/js/plugin/leaflet-fullHash.js +++ b/js/plugin/leaflet-fullHash.js @@ -141,7 +141,7 @@ if (!added) { // if we couldn't add layers (removed or invalid name), add the default one - layersControl.activateFirstLayer(); + layersControl.activateDefaultBaseLayer(); } },