Leaflet / Leaflet.markercluster

Marker Clustering plugin for Leaflet
MIT License
3.92k stars 992 forks source link

position cluster marker based on markers weight #866

Open pank3992 opened 6 years ago

pank3992 commented 6 years ago

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

danzel commented 6 years ago

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.

brendanheywood commented 5 years ago

+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.

2) Have a _defaultMarkerWeightFunction which would simply return 1

3) 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

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

5) 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

danzel commented 5 years ago

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

mrAceT commented 1 year ago

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' :)