Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable android map marker click auto center when using clustering?

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);
        }
    });
like image 611
Ricardo Avatar asked Jan 08 '23 23:01

Ricardo


2 Answers

return true for setOnClusterItemClickListener

like image 168
Tuan Ngb Avatar answered Apr 09 '23 13:04

Tuan Ngb


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;
        }
    });
like image 26
skywall Avatar answered Apr 09 '23 14:04

skywall