Get Position on zooming in map : leaflet

ⅰ亾dé卋堺 提交于 2020-01-15 10:15:06

问题


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?


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



回答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:

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.



来源:https://stackoverflow.com/questions/43064954/get-position-on-zooming-in-map-leaflet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!