I would like to use Leaflet's map.eachLayer(...) to do something to every marker in the map. I am not sure how to detect markers, however.

Is there a way to find out of what class the current layer is an instance of?

I guess I could add a custom property to each marker that I create, such as:

L.marker([40.49083 , -108.95966], {layerType: 'marker'}).addTo(map);

And then check if that property exists... but maybe there is a more elegant way?


From the answer to similar question https://stackoverflow.com/questions/18014907/leaflet-draw-retrieve-layer-type-on-drawedited-event one possible solution would be:

map.eachLayer(function (layer) {
    if (layer instanceof L.Marker){
        //Do marker specific actions here
  • Any feedback? Elegant enough? – TomazicM Mar 20 '18 at 17:39
  • I did something along these lines, using instanceof, but apparently I missed something... [check] – BritishSteel Mar 30 '18 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.