1

I'm clustering markers using the google maps api. Since I want a custom icon as a marker, I've created a custom renderer (that extends DefaultClusterRenderer). On onBeforeClusterItemRendered I'm setting the desired icon.

The problem is that I now want to create a custom infowindow. If I do:

googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        // Use default InfoWindow frame
        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        // Defines the contents of the InfoWindow
        @Override
        public View getInfoContents(Marker arg0) {



        }
    });

In the getInfoContents I can customize the infowindow. The problem is that the passed params are of type Marker. Because I'm using clustering I'm using a custom marker (MyMarker) that as info in it. I can't cast Marker to MyMarker nor can change the params to MyMarker. I wan't to do this to extract the info.

How can I still maintain the clustering (here I can access MyMarker info) but creating a custom infowindow?

public MarkerRenderer() {
        super(getActivity(), mGoogleMap, mClusterManager);         

    }



    @Override
    protected void onBeforeClusterItemRendered(MyMarker myMarker, MarkerOptions markerOptions) {
        markerOptions.icon(BitmapDescriptorFactory.fromResource(myMarker.getMarkerImage())).title(
                "This is my custom marker with the default infoWindo");
    }

    @Override
    protected void onBeforeClusterRendered(Cluster<MyMarker> cluster, MarkerOptions markerOptions) {
        super.onBeforeClusterRendered(cluster, markerOptions);
        markerOptions.title("cluster size " + cluster.getSize());

    }
Favolas
  • 6,963
  • 29
  • 75
  • 127

1 Answers1

1

This answer suggests storing a reference to your cluster or marker in your click listener.

map.setOnMarkerClickListener(clusterMgr);
clusterMgr.setOnClusterClickListener(new OnClusterClickListener<MarkerItem>() {
    @Override
    public boolean onClusterClick(Cluster<MarkerItem> cluster) {
        clickedCluster = cluster; // remember for use later in the Adapter
        return false;
    }
});
clusterMgr.setOnClusterItemClickListener(new OnClusterItemClickListener<MarkerItem>() {
    @Override
    public boolean onClusterItemClick(MarkerItem item) {
        clickedClusterItem = item;
        return false;
    }
});

Then you can use that reference in your adapter.

class MyCustomAdapterForClusters implements InfoWindowAdapter {
@Override
public View getInfoContents(Marker marker) {
    if (clickedCluster != null) {
        for (MarkerItem item : clickedCluster.getItems()) {
            // Extract data from each item in the cluster as needed
        }
    }
    // build your custom view
    // ...
    return view;
}
}

The sample code refers to an InfoWindowAdapter, but it should work as well for a MarkerRenderer; you could pass your custom marker as a parameter to the constructor.

Community
  • 1
  • 1
Beer Me
  • 492
  • 1
  • 7
  • 15