Search code examples
javascriptjquerytoggleleafletgeojson

Toggle layers on and off in Leaflet (more complex scenario)


I am using jQuery's getJSON method to load external line data I've created in QGIS.

What I'm trying to do is toggle my layers on and off - simple check boxes, no radio button for the basemap. I'd also like all the layers to be off when the map is initially loaded.

My code

var map=L.map('map').setView([41.9698, -87.6859], 12);

var basemap = L.tileLayer('http://a.tile.stamen.com/toner/{z}/{x}/{y}.png',
    {
      //attribution: would go here
      maxZoom: 17,
      minZoom: 9
    }).addTo(map);

//display geoJson to the map as a vector

var x = function(source, map)
{
var layers = L.geoJson(source,
    {

style: function(feature){
var fillColor, side=feature.properties.side;
    if (side==='Both') fillColor = '#309e2d';
    else if (side==='Neither') fillColor = '#d90f0f';
    else if (side==='West Only') fillColor = '#e27f14';
    else if (side==='East Only') fillColor = '#2b74eb';
    else if (side==='North Only') fillColor = '#eae42b';
    else if (side==='South Only') fillColor = '#552d04';
    else fillColor = '#f0f5f3';
    return { color: fillColor, weight: 3.5, opacity: null };
        },

onEachFeature: function(feature, geojson){
var popupText="<h1 class='makebold'>Border: </h1>"+feature.properties.name+"<br/>"+"<h1 class='makebold'>Which Side?: </h1>"+feature.properties.side;
geojson.bindPopup(popupText);
        }

    }).addTo(map);
};      

$.getJSON("data/Knox.geojson", function(source){ x(source, map); });
$.getJSON("data/abc.geojson", function(source){ x(source, map); });
$.getJSON("data/xyz.geojson", function(source){ x(source, map); });

I tried assigning a variable before the L.geoJson function (var layers), and then L.control.layers(null, layers).addTo(map); That doesn't seem to work.

How does one create a layer control for multiple external geojson's that are already associated with a few callback functions (L.geoJson, style, and onEachFeature)? Thanks in advance.


Solution

  • EDIT:

    Since you clarified that you want just the entire collection to be switched on/off, it is even more simple (and almost like what you tried by assigning your L.geoJson to var layers), but you have to take care of asynchronous processes.

    To avoid this issue, you could do something like:

    var myLayerGroup = L.layerGroup(), // do not add to map initially.
        overlays = {
            "Merged GeoJSON collections": myLayerGroup
        };
    
    L.control.layers(null, overlays).addTo(map);
    
    function x(source, map) {
        // Merge the GeoJSON layer into the Layer Group.
        myLayerGroup.addLayer(L.geoJson({}, {
            style: function (feature) { /* … */ },
            onEachFeature: function (feature, layer) { /* … */ }
        }));
    }
    
    $.getJSON("data/Knox.geojson", function(source){
        x(source, map);
    });
    

    Then myLayerGroup will be gradually populated with your GeoJSON features, when they are received from the jQuery getJSON requests and they are converted by L.geoJson.


    If my understanding is correct, you would like the ability to switch on/off independently each feature from your GeoJSON data?

    In that case, you would simply populate your layers object while building the L.geoJson layer group, e.g. inside the onEachFeature function:

    var layers = {};
    
    L.geoJson(source, {
    
        style: function (feature) { /* … */ },
    
        onEachFeature: function(feature, layer){
            var popupText = "<h1 class='makebold'>Border: </h1>" +
                    feature.properties.name + "<br/>" +
                    "<h1 class='makebold'>Which Side?: </h1>" +
                    feature.properties.side;
    
            layer.bindPopup(popupText);
    
            // Populate `layers` with each layer built from a GeoJSON feature.
            layers[feature.properties.name] = layer;
        }
    
    });
    
    var myLayersControl = L.control.layers(null, layers).addTo(map);
    

    If you have more GeoJSON data to load and to convert into Leaflet layers, simply do exactly the same (adding built layer into layers in onEachFeature function) and build the Layers Control only once at the end, or use myLayersControl.addOverlay(layer).

    Note: make sure to structure your code to take into account your several asynchronous processes, if you load each GeoJSON data in a separate request. Refer to jQuery Deferred object. Or simply create your Layers Control first and use the addOverlay method.

    If you want them to be initially hidden from the map, simply do not add the geoJson layer to the map…