Skip to content Skip to sidebar Skip to footer

Get Position On Zooming In Map : Leaflet

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 th

Solution 1:

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){
  varmap = e.target,
  bounds = map.getBounds();
  map.eachLayer(function(layer){
    if(bounds.contains(layer.getBounds()))
      alert('kek');
  }); 
})

Solution 2:

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:

functiongetMarkersInside(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.

Post a Comment for "Get Position On Zooming In Map : Leaflet"