4

I know I can avoid centering the camera when tapping a marker using:

mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        marker.showInfoWindow();
        return true;
    }
});

However, the problem I have is that now I want to use the clustering, and of course I want to see the info window when marker (actually, item or cluster) is taped. But I have no idea about how to call something like showInfoWindow in those cases. In this code, if I return false, I see the info window but map is centered. If I return true, map is not centered but info window is not shown:

    mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<HotspotMarker>() {
        @Override
        public boolean onClusterItemClick(HotspotMarker item) {
            Log.e(LOG_TAG, "---OnClusterItemClick");
            return false;
        }
    });

    mClusterManager.setOnClusterItemInfoWindowClickListener(new ClusterManager.OnClusterItemInfoWindowClickListener<HotspotMarker>() {
        @Override
        public void onClusterItemInfoWindowClick(HotspotMarker hotspotMarker) {
            Log.e(LOG_TAG, "---onClusterItemInfoWindowClick");
            HotspotDetailActivity.hotspot = hotspotMarker.getHotspot();
            Intent intent = new Intent(getActivity(), HotspotDetailActivity.class);
            startActivity(intent);
        }
    });

    mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<HotspotMarker>() {
        @Override
        public boolean onClusterClick(Cluster<HotspotMarker> cluster) {
            Log.e(LOG_TAG, "---OnClusterClickListener");
            return false;
        }
    });

    mClusterManager.setOnClusterInfoWindowClickListener(new ClusterManager.OnClusterInfoWindowClickListener<HotspotMarker>() {
        @Override
        public void onClusterInfoWindowClick(Cluster<HotspotMarker> cluster) {
            Log.e(LOG_TAG, "---onClusterInfoWindowClick");
            ArrayList<HotspotMarker> markers = new ArrayList<>(cluster.getItems());
            ClusterListActivity.markers = markers;
            Intent detailHotspot = new Intent(getActivity(), ClusterListActivity.class);
            startActivity(detailHotspot);
        }
    });

Do you have any idea about how can I avoid centering the map when I use clustering and at the same time the info window is shown? Thanks in advance.

EDIT: Using @skywall suggestion, I found even a better way using the custom renderer:

mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<HotspotMarker>() {
            @Override
            public boolean onClusterItemClick(HotspotMarker item) {
                Marker marker=mClusteringRenderer.getMarker(item);
                marker.showInfoWindow();
                return true;
            }
        });

    mClusterManager.setOnClusterItemInfoWindowClickListener(new ClusterManager.OnClusterItemInfoWindowClickListener<HotspotMarker>() {
        @Override
        public void onClusterItemInfoWindowClick(HotspotMarker hotspotMarker) {
            HotspotDetailActivity.hotspot = hotspotMarker.getHotspot();
            Intent intent = new Intent(getActivity(), HotspotDetailActivity.class);
            startActivity(intent);
        }
    });

    mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<HotspotMarker>() {
        @Override
        public boolean onClusterClick(Cluster<HotspotMarker> cluster) {
            Marker marker=mClusteringRenderer.getMarker(cluster);
            marker.showInfoWindow();
            return true;
        }
    });

    mClusterManager.setOnClusterInfoWindowClickListener(new ClusterManager.OnClusterInfoWindowClickListener<HotspotMarker>() {
        @Override
        public void onClusterInfoWindowClick(Cluster<HotspotMarker> cluster) {
            ArrayList<HotspotMarker> markers = new ArrayList<>(cluster.getItems());
            ClusterListActivity.markers = markers;
            Intent detailHotspot = new Intent(getActivity(), ClusterListActivity.class);
            startActivity(detailHotspot);
        }
    });
Ricardo
  • 2,831
  • 4
  • 29
  • 42
  • Unfortunately, it looks like this is not possible. I just tried a few things, like overriding the `onMarkerClick()` for `mClusterManager`, but there's no way to prevent it from centering. The default code is `return this.getMarkerManager().onMarkerClick(marker);`, and if it doesn't get called in this way, the click event doesn't work. – Daniel Nugent Jul 17 '15 at 09:34
  • Great solution. I expect, Google map clusters and cluster items to markers, but I didn't expect it in renderer :-) Thanks. – skywall Jul 17 '15 at 15:58
  • I tried to move those listeners inside the renderer (it is possible) because there are available the methods that you suggest (see it inside the renderer), but for one reason I don't know, it didn't worked. If you finds it, tell me about it. Thanks. – Ricardo Jul 17 '15 at 17:08

3 Answers3

7

return true for setOnClusterItemClickListener

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Tuan Ngb
  • 71
  • 2
  • 1
2

Do not set new instance of GoogleMap.OnMarkerClickListener() directly through mGoogleMap.setOnMarkerClickListener().

Instead, set mClusterManager as click handler like this:

mGoogleMap.setOnMarkerClickListener(mClusterManager);

and handle clicks in:

mClusterManager.setOnClusterItemClickListener();
mClusterManager.setOnClusterClickListener();

InfoWindow is by default shown on first tap.

EDIT: I now understand, where the problem is. I've created not really beautiful solution, but it works. Modify your click listeners this way:

mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyClusterItem>() {
        @Override
        public boolean onClusterItemClick(MyClusterItem item) {
            for (Marker marker : mClusterManager.getMarkerCollection().getMarkers()) {
                if (marker.getPosition().latitude == item.getPosition().latitude && 
                    marker.getPosition().longitude == item.getPosition().longitude) {
                    marker.showInfoWindow();
                }
            }
            return true;
        }
    });
skywall
  • 3,956
  • 1
  • 34
  • 52
  • I think that's how I have configured my map right now, after adding the clustering. The second piece of code has also this: mGoogleMap.setOnMarkerClickListener(mClusterManager); mGoogleMap.setOnInfoWindowClickListener(mClusterManager); So, how can I avoid centering? Thanks. – Ricardo Jul 17 '15 at 08:57
  • Thanks @skywall, see my comments I added to the question. – Ricardo Jul 17 '15 at 11:53
0

I can not disable, but I can move a map below (it need in my case, because upper half of screen covered by some drawers).

public View getInfoContents(Marker marker) {
    .....
    bottomBarButton1.postDelayed(new Runnable() {
        @Override
        public void run() {
            final CameraPosition cp=map.getCameraPosition();
            final Point mappoint = map.getProjection().toScreenLocation(new LatLng(cp.target.latitude, cp.target.longitude));
            mappoint.set(mappoint.x, (int) (dispHeight*0.1f));
            map.animateCamera(CameraUpdateFactory.newLatLng(map.getProjection().fromScreenLocation(mappoint)), null);
        }
    },500);
    .....

}

djdance
  • 3,110
  • 27
  • 33