ashu0047
ashu0047

Reputation: 21

Is it possible to get list of current markers on leaflet Map?

Example I have create 6 markers on and after zoom change on the current view of map I have 3 markers So how can I get list of current onscreen markers list after zoom change?

 map.on('zoomend', function(e) {
     // want to get current onscreen markers list
 });

Upvotes: 1

Views: 4740

Answers (2)

peeebeee
peeebeee

Reputation: 2618

Here's one way of doing it..... iterate the layers on the map, check each one for being a marker and then for being within the current bounds.

function getVisibleMarkers(map) {
    var markerList = [];
    map.eachLayer(function(layer) {
        if ((layer instanceOf L.Marker) && (map.getBounds().contains(layer.getLatLng())){
            markerList.push(layer);
        };
    };
return markerList;
}

Upvotes: 4

ghybs
ghybs

Reputation: 53215

Loop on your 6 Markers.

For each Marker, check if it is within current map view port: Check if marker is in view (map) - mapbox

Upvotes: 1

Related Questions