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

148
Vistas
Remove previously selected geoJSON layer when selection changes

I have a Leaflet map with a select tag that, when a country is selected, the border is drawn around it. I can't seem to get it to remove the previous border when the selection changes though.

Here's my code so far:

//When country selected, show its border
  $("#countrySelector").change(function(){
    //Verify the function is picking up the iso_a3 value in the console
    var selectedCountry = $('#countrySelector').val();
    console.log("You have selected the country " + selectedCountry);

    //AJAX call to JSON data
    $.ajax({
      url: "countryBorders.php",
      dataType: "json",
      type: "POST",
      success: function(result) {
        var data = result["data"];

        //Create the polygon/border for the relevant country
        for(let i = 0; i < data.length; i++) {
          if(selectedCountry === result.data[i].properties.iso_a3) {
            var country = result.data[i];
            var countryStyle = {
              color: "green",
              weight: 0.7
            }
            let countryLayer;
            if(countryLayer) {
              map.removeLayer(countryLayer);
            }
            countryLayer = L.geoJSON(country, countryStyle).addTo(map);
          }
        }
      }
    })
  })

Am I doing something wrong towards the end?

I have seen other similar questions but can't seem to apply it to my example exactly.

Any help would be much appreciated.

about 4 years ago · Juan Pablo Isaza
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