Does the Google Maps Android API Utility Cluster Manager have a minimum number of markers before it will create a cluster?

白昼怎懂夜的黑 提交于 2020-05-12 11:11:15

问题


I am using the Google Maps Android API Utility Library to enable clustering in my app. When five or more markers are co-located a cluster is created: enter image description here

Four or fewer similarly co-located markers won't create a cluster: enter image description here

Is this by design or am I doing something wrong, because my maps have 2, 3 or 4 markers that clearly overlap, but won't form a cluster?

The code to demonstrate the problem:

Map layout:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:map="http://schemas.android.com/apk/res-auto"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.SupportMapFragment"/>

Activity:

    package com.example.android.clusteringminima;

    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;

    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.maps.android.clustering.ClusterItem;
    import com.google.maps.android.clustering.ClusterManager;

    public class ClusteringMinimaTest extends FragmentActivity {

    private GoogleMap mMap;
    private ClusterManager<Place> mClusterManager;

    public class Place implements ClusterItem {
        private final LatLng mPosition;

        public Place(double lat, double lng) {
            mPosition = new LatLng(lat, lng);
        }

        @Override
        public LatLng getPosition() {
            return mPosition;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        if (mMap != null) {
            return;
        }
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        if (mMap != null) {
            mClusterManager = new ClusterManager<Place>(this, mMap);
            mMap.setOnCameraChangeListener(mClusterManager);

            // Cluster of 4 or 5
            mClusterManager.addItem(new Place(51.471524, -0.454280));
            mClusterManager.addItem(new Place(51.471311, -0.452257));
            mClusterManager.addItem(new Place(51.471510, -0.453514));
            mClusterManager.addItem(new Place(51.471710, -0.453714));
            // Remove this item to create a group of four
            mClusterManager.addItem(new Place(51.471810, -0.453714));

            // Cluster of 9 (depending on zoom level)
            mClusterManager.addItem(new Place(51.517399, -0.177480));
            mClusterManager.addItem(new Place(51.509899, -0.134180));
            mClusterManager.addItem(new Place(51.495708, -0.144370));
            mClusterManager.addItem(new Place(51.496112, -0.144162));
            mClusterManager.addItem(new Place(51.512458, -0.118644));
            mClusterManager.addItem(new Place(51.518600, -0.081300));
            mClusterManager.addItem(new Place(51.530449, -0.125480));
            mClusterManager.addItem(new Place(51.513008, -0.088430));
            mClusterManager.addItem(new Place(51.505001, -0.086000));

            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.5, -0.35), 9));        
        }
    }
}

回答1:


UPDATE (3 years after original answer was posted :D)

As pointed out by @schaenk, DefaultClusterRenderer now has setMinClusterSize(int) method which does the same thing.

ORIGINAL ANSWER

The class responsible for making a decision about clustering/not clustering an item is DefaultClusterRenderer. If you look at DefaultClusterRenderer#shouldRenderAsCluster() method - you will see that it starts clustering only when size of cluster is > MIN_CLUSTER_SIZE. In our case MIN_CLUSTER_SIZE = 4.

Unfortunately you cannot change this constant dynamically, so you need to extend DefaultClusterRenderer class and override shouldRenderAsCluster() method to provide your own logic:

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

private void setUpMapIfNeeded() {
    if (mMap != null) {
        return;
    }
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    if (mMap != null) {
        mClusterManager = new ClusterManager<Place>(this, mMap);
        mClusterManager.setRenderer(new CustomRenderer<Place>(this, mMap, mClusterManager));
        ........
    }
}


来源:https://stackoverflow.com/questions/23363188/does-the-google-maps-android-api-utility-cluster-manager-have-a-minimum-number-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!