9

I'm using Google Maps Android API Utility Library in order to show several markers in a map in a clustered way. I've followed the instructions to make it work, as well as taking a look at the examples in the library, but I can't figure out how to show an InfoWindow when a marker is clicked.

I guess getMap().setOnMarkerClickListener(mClusterManager); is the one managing the onClick events, and if commented out, I can override it using getMap().setInfoWindowAdapter(new InfoWindowAdapter() {)); but I have no access to my custom marker object. Nevertheless, if I use getMap().setOnMarkerClickListener(mClusterManager);, I can't find a way to show the InfoWindow when a marker has been clicked.

Does anybody have any idea about how to achieve this?

Thanks a lot in advance!

rciovati
  • 27,603
  • 6
  • 82
  • 101
noloman
  • 11,411
  • 20
  • 82
  • 129
  • You mean a balloon like this? http://stackoverflow.com/questions/3880623/how-to-show-a-balloon-above-a-marker-in-a-mapactivity-isnt-there-a-widget – Tristan Feb 19 '14 at 10:03
  • http://stackoverflow.com/questions/15899619/opening-infowindow-automatically-when-adding-marker-google-maps-v2-android Think this will help you. – Tristan Feb 19 '14 at 10:05
  • @Tristan the first link is deprecated. The second one doesn't work, since the method here is `onClusterClick(Cluster cluster)` and as you see MarkerItem is a custom object of mine and doesn't have the a `showInfoWindow()` method – noloman Feb 19 '14 at 10:09

1 Answers1

23

You need to extend the DefaultClusterRenderer class and override the onBeforeClusterItemRendered, attaching the title to the MarkerOptions object passed as argument.

After that, you can pass your implementation to the ClusterManager.

Example:

class MyItem implements ClusterItem {

    private LatLng mPosition;
    private String mTitle;

    public MyItem(LatLng position){
        mPosition = position;
    }

    @Override
    public LatLng getPosition() {
        return mPosition;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }
}

class MyClusterRenderer extends DefaultClusterRenderer<MyItem> {

    public MyClusterRenderer(Context context, GoogleMap map,
            ClusterManager<MyItem> clusterManager) {
        super(context, map, clusterManager);
    }

    @Override
    protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
        super.onBeforeClusterItemRendered(item, markerOptions);

        markerOptions.title(item.getTitle());
    }

    @Override
    protected void onClusterItemRendered(MyItem clusterItem, Marker marker) {
        super.onClusterItemRendered(clusterItem, marker);

        //here you have access to the marker itself
    }
}

And then you can use it in this way:

ClusterManager<MyItem> clusterManager = new ClusterManager<MyItem>(this, getMap());
clusterManager.setRenderer(new MyClusterRenderer(this, getMap() ,clusterManager));
Muhammad Adil
  • 4,358
  • 3
  • 32
  • 36
rciovati
  • 27,603
  • 6
  • 82
  • 101
  • is there any way to define a custom `InfoWindow` to show, or you can only show the default one? – noloman Feb 19 '14 at 11:23
  • Thanks. Why you call super.onBeforeClusterItemRendered? Apart from, I want to avoid centering the item/cluster in screen when is tap, so I return true in onClusterClick and onClusterItemClick. The problem is that if I return true, the info window is not shown, but it it's false, is shown (but firstly is centered). Any help? Thanks in advance. – Ricardo Jul 16 '15 at 20:15
  • @RiccardoCiovati I am not seeing any info window after implementing this. manulorenzo! did you get the info window? – Rethinavel Dec 29 '15 at 07:53