Geographic Information Systems Asked by Ivan Petrushev on April 24, 2021
I have a simple Marker added to map like this:
var x = L.marker([50.5, 30.5]);
x.addTo(map);
Now how can I hide/remove x
? I searched the API docs for a Marker.remove() method, but couldn’t find any.
Even more odd – Google is failing me. All results I can get is how to remove the whole layer with all markers, but not only one.
Am I getting something wrong here about markers? Is there a way to remove only one?
Yes, it might sound confusing but internally markers are treated like layers. You have to use removeLayer
, it will still only remove a single marker:
var x = L.marker([51.5, -0.09]).addTo(map);
var y = L.marker([51.45, -0.09]).addTo(map);
map.removeLayer(x);
Correct answer by chrki on April 24, 2021
First, adding a lot of markers:
for (var i=0; i < schools.length;i++){
var marker = L.marker([schools[i].latLng.lat, schools[i].latLng.lng], {
icon: L.mapbox.marker.icon({
'marker-color': '#9c89cc'
})
})
.bindPopup('<div class="marker-title"><h2>'+schools[i].uniName+'</h2></div>')
.addTo(map);
allMarkers.push(marker);
}
Then hiding that specific marker (in this case I just hide the first, but you could hide based on any number of variables):
$(allMarkers[0]._icon).hide()
Answered by maudulus on April 24, 2021
Note this answer is outdated. The current behaviour would be x.remove()
https://docs.mapbox.com/mapbox-gl-js/api/markers/#marker#remove
Answered by R. Jutras on April 24, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP