Android open InfoWindow on cluster marker

I implemented a option to find the nearest marker from the actual position. I have all the markers in a ClusterItem with a custom InfoWindow. On a normal google map without clustering I could just use marker.showInfoWindow(); and the InfoWindow would popup. It seems there is no such method when using clustering, because the markers don't get added as proper maps markers.

My code:

public class StationsFragment extends Fragment implements OnMapReadyCallback {
    private static GoogleMap googleMap;

    private ClusterManager<MyItem> clusterManager;
    private MyItem clickedClusterItem;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.main_activity, container, false);

    public void onActivityCreated(Bundle savedInstanceState) {


        final SupportMapFragment map = (SupportMapFragment) getChildFragmentManager().findFragmentById(;

    public void onMapReady(final GoogleMap map) {
        googleMap = map;

        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(0.0, 0.0), 10));

        clusterManager = new ClusterManager<>(getActivity(), map);


        clusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
            public boolean onClusterItemClick(MyItem item) {
                clickedClusterItem = item;
                return false;


    private void loadMarkers() {
        clusterManager.addItem(new MyItem(lat, lng, title, snippet));

    public class ItemAdapter implements GoogleMap.InfoWindowAdapter {

        private final View view;

        ItemAdapter() {
            view = getActivity().getLayoutInflater().inflate(R.layout.info_window, null);

        public View getInfoWindow(Marker marker) {
            TextView title = (TextView) view.findViewById(;

            TextView snippet = (TextView) view.findViewById(;

            return view;

        public View getInfoContents(Marker marker) {
            return null;

    public class MyItem implements ClusterItem {
        private final LatLng position;
        private final String title;
        private final String snippet;

        public MyItem(double lat, double lng, String title, String snippet) {
            this.position = new LatLng(lat, lng);
            this.title = title;
            this.snippet = snippet;

        public LatLng getPosition() {
            return position;

        public String getTitle(){
            return title;

        public String getSnippet(){
            return snippet;


It seems you have not clusterManager.cluster() method of ClusterManager you need to add call this method once marker Items added to the clusterManager

for cluster customization please find below code:

    private class CustomMapClusterRenderer<T extends ClusterItem> extends DefaultClusterRenderer<T> {
            CustomMapClusterRenderer(Context context, GoogleMap map, ClusterManager<T> clusterManager) {
                super(context, map, clusterManager);

            protected boolean shouldRenderAsCluster(Cluster<T> cluster) {
                //start clustering if 2 or more items overlap
                return cluster.getSize() >= Constants.MINIMUM_CLUSTER_SIZE;

            protected void onBeforeClusterItemRendered(T item,
                                                       MarkerOptions markerOptions) {
                ClusterMarkerItem markerItem = (ClusterMarkerItem) item;
            protected void onBeforeClusterRendered(Cluster<T> cluster, MarkerOptions markerOptions) {
// for dynamically change custom map markericon
                BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.drawable.pin_cluster);

            protected void onClusterRendered(Cluster<T> cluster, Marker marker) {
                super.onClusterRendered(cluster, marker);
    //add infowindow to cluster icon
                marker.setSnippet("Total Count - " + cluster.getItems().size());

You can show info window on clustered item, by overriding Renderer:

override fun onClusterItemRendered(clusterItem: MarkerItem?, marker: Marker?) {
        super.onClusterItemRendered(clusterItem, marker)

Please check, this will display infoWindow for all one by one and only last item window will be visible

Need Your Help

How to tell google this text is part of another article

html html5 seo web-crawler google-crawlers

After every article in my website there are previews for other articles. They are random previews.