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

79
Visualizações
Clear GeoJSON layer every time function is called

The function getCountryBorders() updates the map by drawing the desired country border every time the function is run. However, it just adds each country border to the current layer, rather than replacing the old border.

How can I clear the current layer each time the function is called?

const getCountryBorders = function() {
    $.ajax({
        url: "libs/php/getCountryBorders.php",
        type: 'POST',
        dataType: 'json',
        success: function(result) {

            const countries = result["data"]["features"];


            function countryFilter(feature) {
                if (feature.properties.iso_a2 === countryCode) return true
            }

            const borderStyle = {
                "color": "red"
            }

            borderLayer = L.geoJson(countries, {
                filter: countryFilter,
                style: borderStyle
            }).addTo(map);

        },
        error: function(jqXHR, textStatus, errorThrown) {
            
        }
    })
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since your borderLayer is in global scope, a very easy solution is to remove it from the map just before reassigning it:

if (borderLayer) {
  borderLayer.remove();
}
borderLayer = L.geoJson(countries).addTo(map);
about 4 years ago · Juan Pablo Isaza Relatório
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