API实用程序集群管理器是否具有最少数量的标记

API实用程序集群管理器是否具有最少数量的标记

本文介绍了在创建集群之前,Google Maps Android API实用程序集群管理器是否具有最少数量的标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Maps Android API实用程序库在我的应用程序中启用群集.当五个或多个标记位于同一位置时,将创建一个群集:

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:

四个或更少的类似的位于同一地点的标记将不会创建聚类:

Four or fewer similarly co-located markers won't create a cluster:

这是设计上的错误还是因为我的地图上有明显重叠的2个,3个或4个标记,但不会形成聚类呢?

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?

演示该问题的代码:

地图布局:

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

推荐答案

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

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

@schaenk指出, DefaultClusterRenderer 现在具有执行相同功能的setMinClusterSize(int)方法.

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

原始答案

负责对项目进行聚类/不聚类的决策类是 DefaultClusterRenderer .如果您查看 DefaultClusterRenderer#shouldRenderAsCluster() 方法-您会看到,只有在簇的大小大于MIN_CLUSTER_SIZE时,它才开始聚簇.在我们的情况下MIN_CLUSTER_SIZE = 4.

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.

不幸的是,您不能动态更改此常量,因此您需要扩展DefaultClusterRenderer类并重写shouldRenderAsCluster()方法以提供您自己的逻辑:

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

这篇关于在创建集群之前,Google Maps Android API实用程序集群管理器是否具有最少数量的标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:44