Favolas
Favolas

Reputation: 7243

Android Google Map Utils Clustering Distance

I'm using Android Google Map utils to enable clustering of my Markers. I'm using 10 Markers

When I press on a button, I call:

mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(getMarkerBoundingBox(), MAP_PADDING));

Eight of my markers are close to a given region so they are clustering and I can see a blue ball with the number eight on the center. The other two markers are far away from the other group but are really close to each other.

I'm now seeing a cluster with the eight markers and far away a single marker. Only if I zoom in in the area that single marker (that in fact are two) I can see both markers.

I want to show the cluster of the eight markers but a cluster of that two.

How can I decrease the distance that a cluster is created? At the limit, if markers are too close, I want clusters to be created at zoom level last but one.

I've tried to change MAX_DISTANCE_AT_ZOOM at NonHierarchicalDistanceBasedAlgorithm.java but with no success. Any ideas??

Upvotes: 3

Views: 5043

Answers (3)

CoolMind
CoolMind

Reputation: 28835

Using a solution of Alexandr Stukalov or Pavel Dudka:

class CustomClusterRenderer<T : ClusterItem>(
    val context: Context,
    val map: GoogleMap,
    clusterManager: ClusterManager<T>
) : DefaultClusterRenderer<T>(context, map, clusterManager) {

    init {
        minClusterSize = 1
    }
}

Usage:

private var clusterManager: ClusterManager<CustomClusterItem>? = null

override fun onMapReady(googleMap: GoogleMap) {
    this.googleMap = googleMap
    ...

    clusterManager = ClusterManager(context!!, googleMap)
    val clusterRenderer = CustomClusterRenderer(context!!, googleMap, clusterManager!!)
    clusterManager!!.renderer = clusterRenderer
}

data class CustomClusterItem(
    private val position: LatLng,
    private val title: String,
    private val snippet: String,
    val id: Int,
    val index: Int
) : ClusterItem {
    override fun getSnippet(): String = snippet

    override fun getTitle(): String = title

    override fun getPosition(): LatLng = position
}

Upvotes: 0

Alexandr St
Alexandr St

Reputation: 11

You can also use setMinClusterSize(1);

public MyIconRendered(Context context, GoogleMap map,
                      ClusterManager<AbstractMarker> clusterManager) {
    super(context, map, clusterManager);
    setMinClusterSize(1);
}

Upvotes: 1

MarionaDSR
MarionaDSR

Reputation: 322

Try to override shouldRenderAsCluster, so clustering starts from just 2 items:

When declaring the cluster manager:

    mClusterManager.setRenderer(new CustomRenderer<YOUR_TYPE>(getActivity(), googleMap, mClusterManager));

class CustomRenderer<T extends ClusterItem> extends DefaultClusterRenderer<T> {
    public CustomRenderer(Context context, GoogleMap map, ClusterManager<T> clusterManager) {
        super(context, map, clusterManager);
    }

    @Override
    protected boolean shouldRenderAsCluster(Cluster<T> cluster) {
        //start clustering if at least 2 items overlap
        return cluster.getSize() > 1;
    }
 ...
}

Upvotes: 10

Related Questions