Get a list of markers/layers within current map bounds in Leaflet

前端 未结 4 1330
面向向阳花
面向向阳花 2020-12-31 03:46

This is somewhat similar to the question asked here --

I\'m writing a search box for a map application, which retrieves a whole set of search results (people\'s name

4条回答
  •  情话喂你
    2020-12-31 03:55

    I think this may be of help: https://github.com/stefanocudini/leaflet-list-markers

    as you can see from the demo, including all markers in a layer, this plugin shows a list of only those visible in the current viewport. Its usage is simple, in a row:

    var markersLayer = new L.LayerGroup();
    map.addControl( new L.Control.ListMarkers({layer: markersLayer}) );
    

    The code for obtain it is like as:

    var layers = L.LayerGroup(), //layers contains all markers..
        contained = [];          //makers in map boundingbox
    
    layers.eachLayer(function(l) {
        if( l instanceof L.Marker && map.getBounds().contains(l.getLatLng()) )
            contained.push(l);
    });
    

提交回复
热议问题