Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
leaflet: removeLayer() work only once in JavaScript

this is the my Map ,my removeLayer in leaflet map remove once (remove Picture ) and as you see in picture it remove marker perfectly but when i try to remove it again it don't! check 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");

        
       
    }
}

thank you very much!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You check if the checkbox checked. It means that you have programmaticly check and uncheck the checkbox. Like that. I commented out the leaflet part.

function myFunction(id) {
  console.log(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")
        checkBox.checked = false;
    } else {
        //marker = L.marker([lat, lon])
        //var x_id = L.stamp(markers);
        //map.removeLayer(markers[id]);
        console.log("NOOOOT Check");               
        checkBox.checked = true;
    }
}
<button onclick="myFunction('vehicle1')">klick</button>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!