我正在尝试为 Google 地图使用两个 ClusterManager,但我只能添加一个 ClusterManager 及其项目单击事件,
googleMap.setOnMarkerClickListener(mClusterManager);
和
mClusterManager.setOnClusterItemClickListener(new OnClusterItemClickListener<Person>() {
@Override
public boolean onClusterItemClick(Person item) {
Log.v("Cluster Click", "Item Click:"+item.getName());
return true;
}
});
如何向 googlemap 添加一个集群管理器?
提前致谢。
你可以像这样使用MarkerManager:
MarkerManager markerManager = new MarkerManager(mMap);
mClusterManager1 = new ClusterManager<>(getContext(), mMap,markerManager);
mClusterManager2 = new ClusterManager<>(getContext(), mMap,markerManager);
mMap.setOnMarkerClickListener(markerManager);
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
mClusterManager1.onCameraChange(cameraPosition);
mClusterManager2.onCameraChange(cameraPosition);
}
});
mClusterManager1.setRenderer(myClusterRenderer1 = new MyClusterRenderer(getContext(), mMap, mClusterManager1));
mClusterManager2.setRenderer(myClusterRenderer2 = new MyClusterRenderer(getContext(), mMap, mClusterManager2));
是的,将点击侦听器委托给两个集群管理器。
map.setOnClickListener(new GoogleMap.OnMarkerClickListener() {
boolean onMarkerClick(Marker m) {
clusterManager1.onMarkerClick(m)
clusterManager2.onMarkerClick(m)
}
});
Here is how I did it: KOTLIN version
override fun onMapReady(googleMap: GoogleMap?) {
this.googleMap = googleMap
//Step 1
val markerManager = MarkerManager(googleMap) // your googleMap instance
//Step 2
mEquipmentClusterManager = ClusterManager(activity, googleMap, markerManager)
mCustomerClusterManager = ClusterManager(activity, googleMap, markerManager)
googleMap.setOnMarkerClickListener(markerManager)
googleMap.setOnCameraIdleListener(this)
//Step 3
val rendererCustomer =CustomerAroundMeMapPinsCluster(activity,googleMap, mCustomerClusterManager)
val rendererEquipmentRental = EquipmentAroundMeMapPinsCluster(activity,googleMap,mEquipmentClusterManager)
//Step 4
mCustomerClusterManager.setOnClusterClickListener {
Toast.makeText(context, "Customer Cluster click", Toast.LENGTH_SHORT).show()
onClusterClick(it)
return@setOnClusterClickListener false
}
mCustomerClusterManager.setOnClusterItemClickListener {
Toast.makeText(context, "Customer item click", Toast.LENGTH_SHORT).show()
return@setOnClusterItemClickListener false
}
mEquipmentClusterManager.setOnClusterClickListener {
Toast.makeText(context, "Equipment Cluster click", Toast.LENGTH_SHORT).show()
return@setOnClusterClickListener true
}
mEquipmentClusterManager.setOnClusterItemClickListener {
// there is a ticket for this
Toast.makeText(context, "Equipment item clicked", Toast.LENGTH_SHORT).show()
return@setOnClusterItemClickListener false
}
mCustomerClusterManager.renderer = rendererCustomer
mEquipmentClusterManager.renderer = rendererEquipmentRental
}
override fun onCameraIdle() {
mEquipmentClusterManager.cluster()
mCustomerClusterManager.cluster()
}
我知道这可能会迟到,但它可能会对将来遇到这个记录不足的问题的人有所帮助。
要成功将多个集群添加到独立工作的Google地图,最重要且经常被忽视的步骤是添加:
MarkerManager markerManager = new MarkerManager(gMap);
然后,您可以使用此 MarkerManager 创建集群:
ClusterManager<MyClusterItemA> clusterManagerA = new ClusterManager<>(this, gMap, markerManager);
ClusterManager<MyClusterItemB> clusterManagerB = new ClusterManager<>(this, gMap, markerManager);
请注意,两个集群都是使用相同的 MarkerManager 创建的,这是必不可少的。如果您创建两个不同的 MarkerManager 实例,则只有您定义的最后一个 ClusterManager(在本例中为 clusterManagerB)将处理点击和其他交互,从而有效地忽略第一个。
我不是 100% 确定为什么会发生这种情况,但我有根据的猜测(我大约有 90% 的信心)是,当您创建一个与地图绑定的 ClusterManager 而不像这样传递 MarkerManager 时:
ClusterManager<MyClusterItemA> clusterManagerA = new ClusterManager<>(this, gMap);
它默认创建一个新的 MarkerManager,如构造函数中所示:
public ClusterManager(Context context, GoogleMap map) {
this(context, map, new MarkerManager(map));
}
这意味着之前与地图关联的 MarkerManager 被有效替换(如 new MarkerManager(map) 所示),捕获所有点击事件。
但是,如果您使用传递现有 MarkerManager 的构造函数,那么它只是将新集群中的点击添加到同一个 MarkerManager。以下是构造函数代码供参考:
public ClusterManager(Context context, GoogleMap map, MarkerManager markerManager) {
mMap = map;
mMarkerManager = markerManager;
mClusterMarkers = markerManager.newCollection();
mMarkers = markerManager.newCollection();
mRenderer = new DefaultClusterRenderer<>(context, map, this);
mAlgorithm = new ScreenBasedAlgorithmAdapter<>(new PreCachingAlgorithmDecorator<>(
new NonHierarchicalDistanceBasedAlgorithm<T>()));
mClusterTask = new ClusterTask();
mRenderer.onAdd();
}
因此,关键要点是在创建多个集群之前创建一个共享的 MarkerManager,并将相同的 MarkerManager 传递给每个 ClusterManager。