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

78
Views
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 answers
Answer question

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 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!