问题
So I have a couple of stores as layers on my map. All my stores are added to the MarkerCluster and that one is added to map, everything works fine. But I want to display a short info about the store when it's in my viewPort.
With usual stores (layers) this was working first try, but the clustered Group does not work :( I started with this one:
map.eachLayer(function(layer)
but it did not just give me the stores, but alot of things. So I went further to:
if(map.getBounds().contains(layer.getLatLng()))
and that worked as intended.
Not so with Clusterers. I tried iterating over things like that:
$.each(layer._childClusters[0]._markers, function()
{
var element = this;
if(map.getBounds().contains(element.getLatLng()))
{ ....
But it just won't work. Sometimes I get the clusteredGroup (consisting of 4 stores) as 1 layer ... and one store. Sometimes 40.
What is the best way to determine the stores inside of the cluster itself ?
回答1:
You should be able to loop through the individual child Markers in your Marker Cluster Group, or to get an array of those child Markers. Then you can do like in your first example, possibly filtering those within viewport like in your 2nd example.
const mcg = L.markerClusterGroup();
mcg.addLayers(arrayOfMarkers);
// Loop through the child Markers:
mcg.eachLayer(function (layer) {
if (map.getBounds().contains(layer.getLatLng())) {
// Do something...
}
});
// or get all child layers:
const childMarkers = mcg.getLayers();
来源:https://stackoverflow.com/questions/58707554/count-elements-in-markercluster