1

Currently trying to use the following code to add and remove Legends as a result of an overlay being added to the map. Getting no error messages in browser but still doesn't seem to be working.

        map.on('overlayadd', function (eventLayer) {
    if (eventLayer.name === 'Bombing') {
        BombingL.addTo(map);
        map.removeControl(ArmedAssaultL);
    } else {
        map.removeControl(ArmedAssaultL);
    }
    });

in this case I'm trying to say that when a layer is added to the map called 'Bombing', also add the bombing layer legend (BombingL) and remove the Armed Assault Layer legend (Armed AssaultL)

If there is nothing wrong with that code i'll try and get a jsfiddle working to evidence it better.

  • 1
    There is nothing wrong in your code, except maybe that ArmedAssaultL control seems to be never brought back. Could you kindly explain what "doesn't seem to be working"? – ghybs Jan 9 '16 at 11:19
  • Hmm, i have 8 legends defined as variables that link to each of the 8 overlays and just want each legend to come up when prompted and the previous to be removed. Currently when changing the overlay there is no change in Legend. Nothing appears or disappears. – Jamie2483 Jan 9 '16 at 13:36
  • 1
    Well at this point we probably need more of your code to help you further… the JSFiddle would be perfect. – ghybs Jan 9 '16 at 13:42
  • Tried to get it running as a jsfiddle but proved too hard with the external files and css's. Have pushed it to my github. Hopefully you should be able to see the code from there. Code concerning toggling the legends is at the bottom. Thank you very much for your help! jamieodonnell.github.io – Jamie2483 Jan 9 '16 at 14:13
2

You passed your overlays object as first argument of your Layers Control, which makes it a list of base layers (only one at a time can be selected). This is indeed probably what you need, as your data does not seem to be readable if more than one of those layers is shown.

// First argument is for base layers (only 1 at a time can be selected), second argument is for overlays (checkboxes for mutiple selection possible).
L.control.layers(overlays, null, {
  collapsed: false
}).addTo(map);

But then the event that is triggered is "baselayerchange", not "overlayadd":

// Since you use base layers, you should listen to event "baselayerchange" instead of "overlayadd".
map.on('baselayerchange', function(eventLayer) {
  console.log("clicked on base layer: " + eventLayer.name);
  if (eventLayer.name === 'Bombing') { // make sure you compare with the name in the Layers Control (like the code you posted in your question on GIS Stack Exchange), not the name of your variable.
    BombingL.addTo(map);
    map.removeControl(ArmedAssaultL); // You should write a function to remove the previously shown control, or more simply all other legend controls (Leaflet will not trigger an erro if you try to remove something that is not there anyway)
  } else {
    map.removeControl(ArmedAssaultL);
  }
});

Demo on Plunker: http://plnkr.co/edit/a8tRcba0kr3sLeYNuDPC?p=preview

  • Hero! Thanks ever so much. Would have taken me forever to find that. – Jamie2483 Jan 9 '16 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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