[GIS] Check if layer is a marker when using Leaflet’s eachLayer

leaflet

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?

Best Answer

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
    }
});