Reputation: 265
I am fairly new in using leaflet. Can someone tell me that can I get longitude and latitude of the area where I have finished zooming? and How can I know how many objects are in that area?
Upvotes: 1
Views: 2382
Reputation: 76424
Let's suppose you have an L.map object called map
. In this case map.getBounds()
will return you an o.LatLngBounds object, which has a _southWest
and a _northEast
member. These are defining the left-bottom and right-top geographic points of the bounding rectangle. If you have a set of markers, then iterating the set of markers and checking whether they are inside the bounds looks like this:
function getMarkersInside(bounds, markers) {
var subset = [];
for (var markerIndex in markers) {
if ((markers[markerIndex].lat >= bounds._northEast.lat) && (markers[markerIndex].lat <= bounds._southWest.lat) && (markers[markerIndex].lon >= bounds._northEast.lon) && (markers[markerIndex].lon <= bounds._southWest.lon)) {
subset.push(markers[markerIndex]);
}
}
return subset;
}
And you can call it like this:
var markersInside = getMarkersInside(map.getBounds(), myMarkers);
Make sure you do this on zoomend
.
Upvotes: 1
Reputation: 1036
First you need to catch zoomend
event, inside an event object will be map instance, from which you can get boundingBox.
Then for you check each layer is it inside bbox.
map.on('zoomend',function(e){
var map = e.target,
bounds = map.getBounds();
map.eachLayer(function(layer){
if(bounds.contains(layer.getBounds()))
alert('kek');
});
})
Upvotes: 2