I am using the clustering utility of Google Maps. The default implementation is pretty OK, but there are many times in which items are grouped into a cluster, even though they are relatively far from each other.
How can I determine the distance between the items from which the items are grouped together? This of course should change based on the zoom level.
The source code is a good place to look! Based on the comment here,
public static final int MAX_DISTANCE_AT_ZOOM = 100; // essentially 100 dp.
things are clustered together if they are roughly 100dp from the "center" of the cluster. Therefore, two points in a cluster should never be more than 200dp apart. Also note, the "center" is not the middle of the cluster, but rather the marker that was chosen as a seed for the cluster. This makes the algorithm sensitive to the order in which items are added. Description of the algorithm from the docs --
- Iterate over items in the order they were added (candidate clusters)
- Create a cluster with the center of the item.
- Add all items that are within a certain distance to the cluster.
- Move any items out of an existing cluster if they are closer to another cluster.
Remove those items from the list of candidate clusters.
Clusters have the center of the first element (not the centroid of the items within it).
@iagreen's answer exactly explains how the distance is determined in response to the question.
The distance can be changed using this code:
NonHierarchicalDistanceBasedAlgorithm<MarkerClusterItem> algorithm = new NonHierarchicalDistanceBasedAlgorithm<>();
algorithm.setMaxDistanceBetweenClusteredItems(50);
mClusterManager.setAlgorithm(algorithm);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With