简单的解决方案
刚遇到同样的问题。解决方案很简单:从您自己的事件侦听器中调用 ClusterManager 侦听器。看概念:
private OnMapReadyCallback listenerMapReady = (googleMap) ->
{
googleMap.setOnCameraIdleListener(() -> {
mClusterManager.onCameraIdle(); //call cluster manager listener
//....your code for setOnCameraIdleListener event
});
googleMap.setOnMarkerClickListener(marker -> {
if (marker != null && ... check if marker is NOT from cluster manager ....) {
//......your code for unclustered markers, added directly to the map
return false;
}
else
return mClusterManager != null && mClusterManager.onMarkerClick(marker); // let cluster manager do his job
});
}
为了确保集群管理器内部不会拦截 onMarkerClick 侦听器,让每次集群管理器完成他的工作后重置侦听器。我已将其放入 OnCameraIdle 事件中:
public class FragmentRoutes extends Fragment {
private MapView mMapView;
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_routes, container, false);
mMapView = rootView.findViewById(R.id.ac_main_map); // map rendering holder
if (mMapView != null)
mMapView.onCreate(savedInstanceState);
//....... other onCreate mess
}
@Override
public void onResume() {
super.onResume();
if (mMapView != null) {
mMapView.onResume();
mMapView.getMapAsync(listenerMapReady);
}
}
// on marker click listener for the map
private GoogleMap.OnMarkerClickListener listenerMarkerClick = marker -> {
//markers which tag is TransportItem were added directly to map, I will take care of them
if (marker != null && marker.getTag() instanceof TransportItem) {
//......do my tasks for clicked transport marker
return false;
}
else {
// other markers are cluster manager markers, process as usual
return mClusterManager != null && mClusterManager.onMarkerClick(marker);
}
}
// listener for map ready event
private OnMapReadyCallback listenerMapReady = (googleMap) ->
{
if (getContext() == null)
return;
mMap = googleMap;
mMap.setOnCameraIdleListener(() -> {
mClusterManager.onCameraIdle();
//....your code for setOnCameraIdleListener event
// now reset marker click listener
mMap.setOnMarkerClickListener(listenerMarkerClick); //reset marker click listener
});
}
}
就这样!快乐的机器人 :)