0

I am using leaflet to build a layer control with three layers all of them shouldn't be on the map simultaneously. A layer should only appear on the map with a click. the code below doesn't work

   var attractionnyc = L.layerGroup();
   var museumsnyc = L.layerGroup();
   var hotelsnyc = L.layerGroup();

    L.control.layers ({}, {
         'Attraction': attractionnyc,
    },  {
        collapsed: False,
    }).addTo(map);

      L.control.layers ({}, {
         'Museum': museumsnyc,
    },  {
        collapsed: False,
    }).addTo(map);

        L.control.layers ({}, {
         'Hotels': hotelsnyc,
    },  {
        collapsed: False,
    }).addTo(map);

1 Answer 1

1

When you do .addTo(map) you are adding it to the map. You need to do some sort of click event, you can set your layer as a variable like attraction and do something like attraction.addEventListener("click", function(){attraction.addTo(map)});

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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