未调用ClusterManager OnClusterClickListener

时间:2017-09-11 17:36:33

标签: android google-maps google-maps-markers markerclusterer

每个人下午好,我使用集群管理器管理我的谷歌地图v2(我正在使用这个库android-maps-utils)我希望在点击标记时和集群管理器点击时获得差异,但是方法没有打电话,所以我的代码出了什么问题,我在这个小问题上花了10天时间,所以请帮忙。

这是我的全部代码:

public class BigClusteringDemoActivity extends BaseDemoActivity implements ClusterManager.OnClusterClickListener,ClusterManager.OnClusterItemClickListener {
    private ClusterManager<MyItem> mClusterManager;

    @Override
    protected void startDemo() {
        getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 10));

        mClusterManager = new ClusterManager<MyItem>(this, getMap());

        getMap().setOnCameraChangeListener(mClusterManager);
        try {
            readItems();
        } catch (JSONException e) {
            Toast.makeText(this, "Problem reading list of markers.", Toast.LENGTH_LONG).show();
        }
        getMap().setOnMarkerClickListener(mClusterManager);

    }


    private void readItems() throws JSONException {
        InputStream inputStream = getResources().openRawResource(R.raw.radar_search);
        List<MyItem> items = new MyItemReader().read(inputStream);
        for (int i = 0; i < 10; i++) {
            double offset = i / 60d;
            for (MyItem item : items) {
                LatLng position = item.getPosition();
                double lat = position.latitude + offset;
                double lng = position.longitude + offset;
                MyItem offsetItem = new MyItem(lat, lng);
                mClusterManager.addItem(offsetItem);
            }
        }
    }


    @Override
    public boolean onClusterClick(Cluster cluster) {
        Log.d("cluster","clicked" + cluster.getItems());
        return false;
    }

    @Override
    public boolean onClusterItemClick(ClusterItem item) {
        Log.d("cluster","clicked" + item.getPosition());
        return false;
    }
}

2 个答案:

答案 0 :(得分:2)

您尚未使用onClick

将ClusterManager连接到地图

你有一个getMap().setOnCameraIdleListener(mClusterManager);

尝试添加这些以及

getMap().setOnMarkerClickListener(mClusterManager);
mClusterManager.setOnClusterClickListener(this);
mClusterManager.setOnClusterItemClickListener(this);`

这将使用您添加的侦听器的工具。

答案 1 :(得分:0)

我设法找到了ClusterManager初始化的序列,以使点击侦听器最终起作用:

1)初始映射

mMap = googleMap

2)初始化ClusterManager

mClusterManager = ClusterManager(requireContext(), mMap)

3)设置Map OnMarkerClickListener

mMap.setOnMarkerClickListener(mClusterManager)

4)初始化ClusterManager

mClusterManager = ClusterManager(requireContext(), mMap)

5)设置群集点击监听器

mClusterManager.setOnClusterItemClickListener {
    println("CLUST ITEM CLICK")
    return@setOnClusterItemClickListener false
}
mClusterManager.setOnClusterClickListener {
    println("CLUST CLICK")
    return@setOnClusterClickListener false
}

6)现在使用自定义渲染初始化时:

mClusterManager.renderer = CustomIconRenderer(requireContext(), mMap, mClusterManager)