Fraeli78
Fraeli78

Reputation: 21

Leaflet remove old layers

If I search for different locations, the map continues to show the old layers. How can I sort out it?

Please find below the code:

        $("#searchBtn").click(function() {

            query = $("#searchInput").val();
            if (query) {

                draw_counties(query, map);
            }

        });

https://jsfiddle.net/Polp026/17ux3gpk/3/

Upvotes: 0

Views: 104

Answers (1)

Fraeli78
Fraeli78

Reputation: 21

I found the solution. Now it works!

        $("#searchBtn").click(function() {
            query = $("#searchInput").val();
            if (query) {
            map.eachLayer(function (layer) {
                map.removeLayer(layer);
                });   
                draw_counties(query, map);
                basemap.addTo(map);
            }    
        });

https://jsfiddle.net/Polp026/17ux3gpk/32/

Upvotes: 1

Related Questions