0

I have a leaflet web map created via qgis2web plugin using qgis software. There is no option in plugin window to generate select/deselect button for the map layer. Now I want to add that button on the map, but I don't know which code part and where I should add it.

closed as off-topic by PolyGeo Mar 1 '18 at 8:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

2

Understanding you want a button for all on, and a button for all off. Not using the Leaflet Layers control.

I've put two buttons outside my map div,

<input type="button" value="Turn on Layers" onclick="turnLayerOn()" />
<input type="button" value="Turn off Layers" onclick="turnLayerOff()" />    

and using a layerList these two functions turn on or off the layers in the list.

////////////////    Turns Layers on/off by button
    var layerList = [ NLTeam, ALTeam ]  // no quotes as layers are objects

    function turnLayerOn(){ 
        for (i = 0; i < layerList.length; i++) { 

            if(!map.hasLayer(layerList[i])) {  //note the ! for off.
                map.addLayer(layerList[i]);
            }
        }

    }
    function turnLayerOff(){ 
        for (i = 0; i < layerList.length; i++) { 

            if(map.hasLayer(layerList[i])) {  
                map.removeLayer(layerList[i]);
            }
        }

    }
/////////////////////////////
0

You should be able to create an html button which has an on click function. i.e.

<input type="button" value="Turn on Layer" onclick="turnLayerOn()" />

Now define a function that turns it on:

function turnLayerOn(){ 
   map.addLayer(waterLayer);
}

Or have a turn off button: which would map.removeLayer(waterLayer);

Another option is to determine if it's added then remove or if not added, then add it, make a function for this and call it on the button click.

if(map.hasLayer(waterLayer)) {  //layer added is true
     map.removeLayer(waterLayer);
}else{
     map.addLayer(waterLayer)
}
  • I think the OP wants a control to toggle all layers. – Tom Chadwin Sep 27 '17 at 15:40
  • He needs a list of layers Then for i in layerList use the map.hasLayer(i) and either addLayer or removeLayer based on it's current status. – Bill Chappell Sep 27 '17 at 17:53
  • I just want to turn on/off all layers checking the box or clicking on the button. – user104851 Sep 28 '17 at 10:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.