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

597
Views
How can I remove existing control in Leaflet?

My target is delete actual control on the map to create it again with updated data.

I know about control.remove() but to use this function I need first get control object.

That's my control legend basic from L.control:

Leaflet control

And here is the code to create L.control:

function legend(map)
{
    var legend = L.control({position: 'bottomright'});
    
    legend.onAdd = function (map) {

        var elements = [];
        
        map.eachLayer(function(layer){
        if (layer._icon)
        {
            elements.push("svg");
        }
        });
        

            var div = L.DomUtil.create('div', 'info legend'),
                elements,
                labels = [];

            for (var i = 0; i < elements.length; i++) {
                    div.innerHTML +=
                        '<i>'elements[i]'</i><br>';
                        
                }

            return div;
        };

    legend.addTo(map);
}

Thank you.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

function legend(map) {
  var legend = L.control({ position: 'bottomright' });

  legend.onAdd = function (map) {

    var elements = [];

    map.eachLayer(function (layer) {
      if (layer._icon) {
        elements.push("svg");
      }
    });


    var div = L.DomUtil.create('div', 'info legend'),
      elements,
      labels = [];

    for (var i = 0; i < elements.length; i++) {
      div.innerHTML +=
        '<i style="' elements[i] '"></i><br>';

    }

    return div;
  };

  legend.addTo(map);
  return legend
}

const legendInstance = legend(map);
// remove 
legendInstance.remove()

you must return and save control instance if you want remove

about 4 years ago · Santiago Gelvez 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!