leaflet: removeLayer() work only once in JavaScript

3241
1
01-08-2022 12:17 AM
Labels (1)
samadalien
New Contributor

  This is my map  ,my removeLayer in leaflet map remove once (Removed picture )and as you see in picture it remove marker perfectly but when I try to remove it again it don't! checked is not true but it didnt remove for 2nd time here is the function that called from checkbox :

 

 function myFunction(id) {
    var marker = [];
    var checkBox = document.getElementById(id)
    var lat = checkBox.name;
    var lon = checkBox.value;
    var RN = checkBox.className;
    console.log(RN)
    
    var pop_cont = `<img id="icon" src="sample.jpg" alt="shipPIC"/><br><b>${RN}</b><br> latitude :${lat}<br>longitude: ${lon}`
    if (checkBox.checked == true) {
        marker = L.marker([lat, lon]).addTo(map);
        marker.setIcon(dish_icon);
        marker.bindPopup(pop_cont).openPopup();
        console.log("checked")

    } else {
        marker = L.marker([lat, lon])
        var x_id = L.stamp(markers);
        map.removeLayer(markers[id]);
        console.log("NOOOOT Check");

        
       
    }
}


 

  why removeLayer() work for first time remove but when i add that layer again and want to remove it , removeLayer don't work?!
thank you verymuch!

0 Kudos
1 Reply
GavinRehkemper
Esri Contributor

Hi, thank you for the question. Could you please create a demonstration example code (you can use this JS Bin as a starter if you'd like) and post it here to help us understand your issue? Thank you.

0 Kudos