I'm filtering a geojson layer by user inputs,and works fine for the first query, and works fine for the first query, the problem is with the second so on. starting from the second query brings me the last layer plus the new one I already tried map.removeLayer(layer) any ideas on how can I get only the new filter?,
function query(data) {
var input=null;
var user = null;
var input = document.getElementById("userInput").value;
departamentos.then(function(data) {
var alldeptos = L.geoJson(data);
user = L.geoJson(data, {
filter: function (feature, layer) {
//console.log(feature)
return feature.properties.nom_dpto == input;
},
});
});
$("#allbus").click(function() {
map.addLayer(alldeptos)
map.removeLayer(user)
});
$("#form").click(function() {
map.removeLayer(alldeptos)
map.removeLayer(user)
map.addLayer(user)
});
});
}
1 Answer 1
I solved this by adding the layer to a new layer group
var fil_dpt = L.layerGroup().addTo(map);
function query(data) {
var input = document.getElementById("userInput").value;
departamentos.then(function(data) {
var alldeptos = L.geoJson(data);
user = L.geoJson(data, {
filter: function (feature, layer) {
//console.log(feature)
return feature.properties.nom_dpto == input;
},
});
$("#allbus").click(function() {
if (map.hasLayer(user)) map.removeLayer(user)
//if (!map.hasLayer(alldeptos)) map.addLayer(alldeptos);
if (map.hasLayer(alldeptos)){ map.removeLayer(alldeptos)}
else {map.addLayer(alldeptos)}
});
$("#form").click(function() {
fil_dpt.clearLayers();
if (map.hasLayer(alldeptos)) map.removeLayer(alldeptos);
//if (!fil_dpt.hasLayer(user)) fil_dpt.addLayer(user);
if (fil_dpt.hasLayer(user)){ fil_dpt.removeLayer(user)}
else {fil_dpt.addLayer(user)}
});
});
}
it was also with the help of deleted answer of @TomazicM first filter second filter
#all
for layeralldepots
, clicking#form
for layercaldas
and clicking#boyaca
for layerboyaca
?