Answer the question
In order to leave comments, you need to log in
How to find and remove leaflet duplicates?
I'm using a leaflet map that dynamically displays markers.
Sometimes there is a problem that there are duplicate markers with the same lat lng.
How can you search for and remove markers from the map with the same lat and lng (accurate to hundredths)?
For example, markers that are outside the visibility of the map when it is moved, I delete like this:
function deletepoints() {
bounds = map.getBounds();
markers.eachLayer(function(layer) {
if(bounds.contains(layer.getLatLng())) {
} else {
markers.removeLayer(layer);
}
});
}
Answer the question
In order to leave comments, you need to log in
We get an array of markers, iterate through it, for each marker we check the presence of a marker with similar coordinates (the absolute value of the difference in coordinates is less than a certain one). If the found marker is not the same as the current checked one, delete the checked one. I put together a small demo - you can place markers, manually and randomly, adjust the allowable difference in coordinates, delete them.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question