Question

I need your help,

I want that in activeLayers = [bus,stops,slot1] rather than activeLayers = ["bus","stops","slot1"] how can I manage this? In other words, I want to "transform" the string to the object, it should reference.

sadly it doesn't work here is some more code maybe it helps

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"});


                        // Wichtige Plätze
                        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 = {
                    "Sonstiges": 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: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                }).addTo(map);
}

here is the URL i use to show the website

campusmap.html?lat=49.99684&lng=8.24811&zoom=18&layers=places,bus,slot1

Was it helpful?

Solution

If I understand your question correctly, you want to be able to reference certain variables in the query string? If so, consider trying this:

var validLayers = {
    slot1: new L.LayerGroup(),
    places: new L.LayerGroup(),
    stops: new L.LayerGroup()
};

// get url parameters
// ...

if( params.layers) {
    var activeLayers = params.layers.split(",").map(function(key) {
        return validLayers[key];
    });
}

Here, validLayers is being used as a whitelist, so only the things you allow are able to be referenced.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top