Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda