如何向 ClusterItem 添加标题、摘要和图标?

How to add title, snippet and icon to ClusterItem?

我有这个activity:

public class MapViewer extends Activity {

    private GoogleMap map;
    private Database db = new Database(this);

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapviewer);

        try {
            map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            if (map != null) {
                map.setMyLocationEnabled(true);
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                map.getUiSettings().setRotateGesturesEnabled(false);

                this.addMerchantMarkers(new MarkerOptions());
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    public void addMerchantMarkers(MarkerOptions mo) {
        SQLiteDatabase dbRead = db.getReadableDatabase();
        Cursor result = dbRead.rawQuery("SELECT title, addr, lat, lon FROM users", null);

        while(result.moveToNext()) {            
            map.addMarker(mo.position(new LatLng(result.getFloat(2), result.getFloat(3)))
                    .title(result.getString(0))
                    .snippet(result.getString(1))
                    );;
        }
    }
}

我通过以下方式更改以使用标记聚类:

public class MapViewer extends Activity {

    private GoogleMap map;
    private Database db = new Database(this);
    private ClusterManager<MyItem> mClusterManager;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapviewer);

        try {
            map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            if (map != null) {
                map.setMyLocationEnabled(true);
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                map.getUiSettings().setRotateGesturesEnabled(false);

                setUpClusterer();
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    private void setUpClusterer() {
        mClusterManager = new ClusterManager<MyItem>(this, map);

        map.setOnCameraChangeListener(mClusterManager);
        map.setOnMarkerClickListener(mClusterManager);

        addItems();
    }

    private void addItems() {
        SQLiteDatabase dbRead = db.getReadableDatabase();
        Cursor result = dbRead.rawQuery("SELECT lat, lon, title, addr FROM users", null);

        while(result.moveToNext()) {
            MyItem offsetItem = new MyItem(result.getFloat(0), result.getFloat(1));
            mClusterManager.addItem(offsetItem);
        }
    }
}

现在我不知道如何像以前的代码一样为每个标记添加标题、片段和图标。 现在,如果我单击单个标记,什么也不会发生…… 如何获得?

在这里您应该使用 ClusterManager 本身。 例如,在集群项上设置点击:

        mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
            @Override
            public boolean onClusterItemClick(MyItem item) {
                //put your code here
                return false;
            }
        });

ClusterManager 中还有其他不同的方法class。 您将需要一个不太明显的代码来将信息 window 分配给标记或集群 - 您应该使用此代码:

mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(new MarkerInfoWindowAdapter());
mClusterManager.getClusterMarkerCollection().setOnInfoWindowAdapter(new ClusterInfoWindow());

MarkerInfoWindowAdapter 和 ClusterWindowAdapter 是您的 classes,它们实现了 GoogleMap.InfoWindowAdapter.

使用图标有点困难,因为群集会将图标更改为默认图标。你应该使用方法:

public void setRenderer(com.google.maps.android.clustering.view.ClusterRenderer<T> view)

您可以使用自己的渲染器 class 并覆盖一些方法。例如,要设置自定义图标,请使用 class:

class OwnIconRendered extends DefaultClusterRenderer<MyItem> {

    public OwnIconRendered(Context context, GoogleMap map,
                           ClusterManager<MyItem> clusterManager) {
        super(context, map, clusterManager);
    }

    @Override
    protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
        markerOptions.icon(item.getIcon());
        markerOptions.snippet(item.getSnippet());
        markerOptions.title(item.getTitle());
        super.onBeforeClusterItemRendered(item, markerOptions);
    }
}

并这样使用:

mClusterManager.setRenderer(new OwnIconRendered(activity.getApplicationContext(), getMap(), mClusterManager));