如何在Android中的普通标记和群集标记之间创建单独的标记单击侦听器

时间:2019-08-02 16:04:54

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

我有一些普通标记和聚类标记。 googleMap.setOnMarkerClickListener(mClusterManager)侦听器会覆盖普通标记侦听器,并且普通标记单击不起作用。因此,我要分别处理两个标记单击。

2 个答案:

答案 0 :(得分:1)

ClusterManager为此具有单独的方法:

setOnClusterItemClickListener单击Marker的句柄

setOnClusterClickListener单击Cluster的句柄

MarkerCluster都是地图上的标记。您指定ClusterManager将管理通过googleMap.setOnMarkerClickListener(mClusterManager)进行的标记点击。

现在您需要为setOnClusterItemClickListener设置setOnClusterClickListenerClusterManager

样品

googleMap.setOnMarkerClickListener(mClusterManager)
mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<ClusterItemClass>(){
  @Override
    public boolean onClusterClick(Cluster<ClusterItemClass> cluster) {
      //logic for handle on cluster click
    }
});
mClusterManager.OnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<ClusterItemClass>(){
  @Override
    public boolean onClusterItemClick(ClusterItemClass item) {
      //logic for handle on cluster item click
    }
});

Documentation中查看更多信息

答案 1 :(得分:-2)

我从How to manage click on marker which is not in Cluster in Android Google Map API?那里得到了解决方法

对于普通标记

  1. googleMap.setOnMarkerClickListener(mClusterManager.getMarkerManager());

  2. MarkerManager.Collection normalMarkersCollection = mClusterManager.getMarkerManager().newCollection();

  3. 创建普通标记

    MarkerOptions markerOptions = MarkerOptions() .position(new LatLng(...)) .title("Title") .snippet("Description")

将普通标记添加到“ normalMarkersCollection”中

normalMarkersCollection.addMarker(markerOptions)
  1. 为普通标记设置点击。

    normalMarkersCollection.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener { public boolean onMarkerClick(marker: Marker) { // NORMAL MARKER CLICKED! return false; } });

用于群集标记

mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<ClusterItemClass>(){
  @Override
    public boolean onClusterClick(Cluster<ClusterItemClass> cluster) {
      //logic for handle on cluster click
    }
});

mClusterManager.OnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<ClusterItemClass>(){
  @Override
    public boolean onClusterItemClick(ClusterItemClass item) {
      //logic for handle on cluster item click
    }
});