Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda