如何管理不在Android Google Map API中的群集中的单击标记? [英] How to manage click on marker which is not in Cluster in Android Google Map API?
问题描述
我无法为不在集群中的标记实现OnClickListener,即未使用以下方式添加标记:
I am having trouble implementing OnClickListener for Marker(s) which are not in Cluster, i.e. not added using:
mClusterManager.addItem(markerCluster);
我将OnMarkerClickLister设置如下:
I have set OnMarkerClickLister as follows:
mMap.setOnCameraIdleListener(mClusterManager);
mMap.setOnMarkerClickListener(mClusterManager);
如果我只是使用:
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
return false;
}
});
点击标记根本不起作用.
the click on markers is not working at all.
例如:我有这种情况:
两个绿点和一个大蓝点(数字6)是一个簇,但是红色标记不在簇中,因为我不希望它像其他标记一样被簇. 当我单击红色标记时,InfoWindow通常会显示,但是onMarkerClick以及OnClusterItemClickListener均不起作用.但是,当我单击群集"中的标记时,OnClusterItemClickListener可以工作.
Two green dots and big blue dot (with number 6) are one Cluster, but the red marker is not in Cluster as I don't want it to be Clustered like other markers. InfoWindow is normally showing when I click on red marker, but onMarkerClick doesn't work - as well as OnClusterItemClickListener. But OnClusterItemClickListener works when I click on markers which are in Cluster.
希望您了解我正在努力实现的目标.如果没有,请告诉我.
Hope you understand what I am trying to achieve. If not, please let me know.
推荐答案
更新后的答案
您在编辑中添加的图像清楚地说明了问题,非常感谢!
The image that you added in your edit explains the issue clearly, thanks for that!
为了同样解决此问题,我们需要以其他方式注册侦听器.即:通过将其注册到ClusterManager的MarkerManager,因为该类现在可以处理所有标记.我们还需要以不同的方式添加NormalMarkers,因此让我们逐步进行操作:
In order too solve this issue we need to register the listener differently. Namely: by registering it to the ClusterManager's MarkerManager as that class handles everything of the markers now. We also need to add the NormalMarkers a bit differently, so lets go through it step by step:
1)更新mMap
的OnMarkerClickListener
:
mMap.setOnMarkerClickListener(mClusterManager.getMarkerManager()); // Note the `MarkerManager` here
2)此MarkerManager
保存所有集合.我们需要在此管理器上创建一个新集合,向该集合中添加应该与集群分开显示的NormalMarkers:
2) This MarkerManager
holds all the collections. We need to create a new collection on this manager to which we will add the NormalMarkers that should be displayed apart from the clusters:
MarkerManager.Collection normalMarkersCollection = mClusterManager.getMarkerManager().newCollection();
3)添加标记的方式与之前类似,但是在我们创建的集合上使用了addMarker()
方法.我们必须将MarkerOptions()
对象传递给此对象:
3) Adding the markers is done similar to how this was before, but with using the addMarker()
method on the collection that we created. We must pass a MarkerOptions()
object to this:
// Create a normal marker
val markerOptions = MarkerOptions()
.position(new LatLng(...))
.title("My marker")
.snippet("With description")
// Add it to the collection
normalMarkersCollection.addMarker(markerOptions)
4)最后但并非最不重要:我们要在其上放置OnClickListener
:
4) Last but not least: we want the OnClickListener
on it:
normalMarkersCollection.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener {
public boolean onMarkerClick(marker: Marker) {
// NORMAL MARKER CLICKED!
return false;
}
})
5)完成!现在,普通标记已像以前一样添加到地图上,但是具有有效的OnMarkerClickListener
.
5) Done! Now the normal markers are added to the map just like before, but with a working OnMarkerClickListener
.
更早的答案
(设置群集和群集项目的点击侦听器)
您应将Clicklistener添加到mClusterManager
.使用ClusterManager时,在mMap
上设置clicklistener无效.
You should add the clicklistener to the mClusterManager
. Setting the clicklistener on the mMap
does not work when using the ClusterManager.
因此,不要使用mMap.setOnMarkerClickListener
,而是在集群管理器上设置ClusterItemClickListener
:
Thus instead of using mMap.setOnMarkerClickListener
, set the ClusterItemClickListener
on the cluster manager:
mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
@Override
public boolean onClusterItemClick(MyItem item) {
Log.d("cluster item","clicked");
return true;
}
});
此外,如果要捕获群集项的onclick,请使用ClusterClickListener
:
Additionally, if you want to capture the onclick of the clustered item item, use the ClusterClickListener
:
mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MyItem>() {
@Override
public boolean onClusterClick(Cluster<MyItem> cluster) {
Log.d("cluster","clicked");
return true;
}
});
这篇关于如何管理不在Android Google Map API中的群集中的单击标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!