I am planning to use the google maps marker clustering available in the utils library, but the google example app only shows marker clusters without any infoWindow. I am wondering now, am I not able to show a InfoWindow? I want the InfoWindow to be displayed on the marker like with a normal google maps marker, not on the cluster.
The code I have: (From the google example)
public class BigClusteringDemoActivity extends FragmentActivity {
private ClusterManager<MyItem> mClusterManager;
private GoogleMap mMap;
private void readItems() {
InputStream inputStream = getResources().openRawResource(R.raw.radar_search);
List<MyItem> items = new MyItemReader().read(inputStream);
for (int i = 0; i < 10; i++) {
double offset = i / 60d;
for (MyItem item : items) {
LatLng position = item.getPosition();
double lat = position.latitude + offset;
double lng = position.longitude + offset;
MyItem offsetItem = new MyItem(lat, lng);
mClusterManager.addItem(offsetItem);
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mClusterManager = new ClusterManager<>(this, mMap);
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 10));
mMap.setOnCameraChangeListener(mClusterManager);
readItems();
}
}
Here is a simplified and slightly modified solution based on this answer. Note that the linked answer implements an InfoWindow for both Markers and Clusters.
This solution only implements InfoWindows for Markers.
It's similar to how you would implement a custom InfoWindowAdapter for normal Markers with no Clustering, but with the additional requirement that you keep a reference to the currently selected Item so that you can get the Title and Snippet from it's
MyItem
instance, since the Marker does not store the Title and Snippet as it usually does.Note that since all of the data is stored in
MyItem
references, it's much easier to extend the functionality to display as many data types as you want in the InfoWindow for each Marker.First, the MyItem.java, which includes extra fields for Title and Snippet:
Here is the full Activity class, which includes all of the functionality to support InfoWindows for each Marker added using the Cluster library:
Edit: Added support for handling click events on the InfoWindow, made the Activity implement
OnClusterItemInfoWindowClickListener
and added theonClusterItemInfoWindowClick
callback.info_window.xml:
Result:
Initial launch:
Zooming out, starts Clustering:
Zooming out again, more Clustering:
Then, zooming in, and clicking on an individual Marker:
Then clicking on another Marker:
Edit: In order to show the "speech bubble" around the custom InfoWindow, use
getInfoContents()
instead ofgetInfoWindow()
:Result: