Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distance based clustering with Google Maps

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.

like image 892
Yonatan Nir Avatar asked Sep 14 '25 16:09

Yonatan Nir


2 Answers

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

  1. Iterate over items in the order they were added (candidate clusters)
  2. Create a cluster with the center of the item.
  3. Add all items that are within a certain distance to the cluster.
  4. Move any items out of an existing cluster if they are closer to another cluster.
  5. 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).

like image 200
iagreen Avatar answered Sep 16 '25 06:09

iagreen


@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);
like image 41
Sethuraman Srinivasan Avatar answered Sep 16 '25 06:09

Sethuraman Srinivasan