在创建群集之前,Google Maps Android API实用程序群集管理器是否具有最少数量的标记?

问题描述 投票:17回答:1

我正在使用Google Maps Android API Utility Library在我的应用中启用群集。当五个或更多标记共同定位时,将创建一个集群:

四个或更少的类似共处标记不会创建一个集群:

这是设计还是我做错了,因为我的地图有2个,3个或4个标记明显重叠,但不会形成群集?

用于演示问题的代码:

地图布局:

<?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"/>

活动:

    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));        
        }
    }
}
android google-maps-android-api-2
1个回答
33
投票

更新(原始答案发布3年后:D)

正如@schaenk所指出的,DefaultClusterRenderer现在有setMinClusterSize(int)方法做同样的事情。

原始答案

负责决定聚类/不聚类项目的类是DefaultClusterRenderer。如果你看一下DefaultClusterRenderer#shouldRenderAsCluster()方法 - 你会看到它只在簇的大小> MIN_CLUSTER_SIZE时才开始聚类。在我们的情况下MIN_CLUSTER_SIZE = 4

遗憾的是,您无法动态更改此常量,因此您需要扩展DefaultClusterRenderer类并覆盖shouldRenderAsCluster()方法以提供您自己的逻辑:

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));
        ........
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.