Can you assist me?
I am trying to have activeLayers = [bus,stops,slot1]
instead of
activeLayers = ["bus","stops","slot1"]
. How can I achieve this? Essentially, I want to convert the string into the object it references.
Unfortunately, my current approach is not working. Here is some additional code that may provide more context:
window.onload = function () {
var validLayers = {
slot1 : new L.LayerGroup(),
places : new L.LayerGroup(),
stops : new L.LayerGroup()
};
var platz = L.MakiMarkers.icon({icon: "embassy", color: "#f00", size: "l"});
var extern = L.MakiMarkers.icon({icon: "college", color: "#FF9900", size: "l"});
// Important Places
L.marker([49.99264845,8.24160625696918], {icon: platz}).bindPopup('Wiese vor dem ZDV').addTo(validLayers.places);
L.marker([49.99454,8.24384], {icon: platz}).bindPopup('Rote Infobox').addTo(validLayers.places);
L.marker([49.99182355,8.23384953468164],{icon:platz}).bindPopup('Zentralmensa').addTo(validLayers.places);
// Get url parameters
var params = {};
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
params[key] = value;
});
if( params.layers) {
var activeLayers = (function(arr) {
var l = arr.length, i, ret = {};
for( i=0; i<l; i++) ret[i] = validLayers[arr[i]];
return ret;
})(params.layers.split(","));
}
// Create map
var map = new L.Map('map', {
center: [params.lat || 49.99238, params.lng || 8.23779],
minZoom: 12,
maxZoom: 18,
zoom: params.zoom || 15,
layers: activeLayers || [validLayers.places, validLayers.stops ,validLayers.slot1]
});
var baseLayers = {
};
var overlays = {
"Other": validLayers.places,
"Bus": validLayers.stops,
"Slot 1": validLayers.slot1,
"Slot 2": validLayers.slot2,
"Slot 3": validLayers.slot3,
"Slot 4": validLayers.slot4
};
L.control.layers(baseLayers, overlays).addTo(map);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
}
Here is the URL I am using to display the website:
campusmap.html?lat=49.99684&lng=8.24811&zoom=18&layers=places,bus,slot1