Leaflet.markercluster icon indicating copy to clipboard operation
Leaflet.markercluster copied to clipboard

position cluster marker based on markers weight

Open pank3992 opened this issue 6 years ago • 4 comments

Hi, I want to position clusters based on the weight of the markers. I looked at the source code but was unable to deduce how to do that. One way is to create that number (weight) of markers at that particular position, but that will significantly reduce the performance. Is there any other way round to achieve the same without much affecting the performance? Thank you

pank3992 avatar Jan 21 '18 16:01 pank3992

You would need to override the MarkerCluster._recalculateBounds method. https://github.com/Leaflet/Leaflet.markercluster/blob/master/src/MarkerCluster.js#L158-L206

There isn't a good way to do this at the moment.

danzel avatar Jan 21 '18 19:01 danzel

+1 and this doesn't seem too hard to me, so I'll probably have a crack at a PR

@danzel can you please sanity check my plans:

  1. Create a companion function option to iconCreateFunction which I'd call markerWeightFunction.

This function is passed the raw marker and must return a positive number so we don't get div by 0 issues. I legitimately do have markers and clusters with 0, but I think we can workaround that by just returning something like 0.01 for a 'weightless' marker and this should still work fine even if all the markers in a cluster are weightless.

  1. Have a _defaultMarkerWeightFunction which would simply return 1

  2. Weight the latSum and lngSum by the weight of the marker here:

https://github.com/Leaflet/Leaflet.markercluster/blob/master/src/MarkerCluster.js#L183-L184

  1. totalCount would need to be calculated rather than use this._childCount

  2. I'm a little hazy on what needs to happen with the recursive child markers, I'm guessing we will need a new this._childWeight to replace this._childCount

https://github.com/Leaflet/Leaflet.markercluster/blob/master/src/MarkerCluster.js#L198-L202

brendanheywood avatar Oct 17 '18 06:10 brendanheywood

Sounds about right, replace childCount with summed child weight and sum up the weighted lat/lng values. :+1:

danzel avatar Oct 17 '18 19:10 danzel

this doesn't seem too hard to me,

It must be harder then is seemed !

I have searched the internet and found several other people asking for this exact functionality, but alas no working solution.

If by any chance someone could find the time to add this functionality I am sure I'm not the only 'happy camper' :)

mrAceT avatar Jul 05 '23 08:07 mrAceT