I'm working with markers in a cluster (google maps), I have no problem in showing an info window when calling the onclick method. The problem is that I can't find how to use the method showInfoWindow() as I do on a marker to open the info without giving click.
When I use a marker
marker = map.addMarker(new MarkerOptions()
.position(position)
.snippet(info));
then I call
marker.showInfoWindow();
how I can do the same with a marker (ClusterItem) that is on the map within the cluster manager?
MarkCluster cluster = new MarkCluster(Lat, Lon, info);
mClusterManager.addItem(cluster);
It is the marker that I want to show infoWindow
Try this, its what I did to get references to map markers when using clustering:
When you create a ClusterManager
it always creates and uses an instance of the DefaultClusterRenderer
if you don't call the .setRenderer()
method and pass it an instance of your own ClusterRenderer
implementation. If you are letting the ClusterManager
create its own DefaultClusterRenderer
the key is to add it explicitly so that you can keep a reference to it (because the ClusterManager
has no getter method so you can get a reference to the ClusterRenderer
its using):
mClusterManager = new ClusterManager<ClusterItem>(getActivity(), mMap);
mRenderer = new DefaultClusterRenderer(getActivity(), mMap, mClusterManager);
mClusterManager.setRenderer(mRenderer);
mClusterManager.addItem(ClusterItem);
Then when you need access to a marker pass the ClusterRenderer
the ClusterItem
associated with the marker. The ClusterItem
you use to find the marker will be the ClusterItem
you passed to the ClusterManager
to add the marker to the cluster originally:
Marker marker = mRenderer.getMarker(ClusterItem);
if(marker != null){
marker.showInfoWindow();
}
The Marker
object will be null if the marker has not been rendered on the map yet so be sure to check that the marker object is not null before using it.
If you are certain the marker has been placed on the map when you call .getMarker()
and marker is still null then override the .equals()
method
in the object use to implement the ClusterItem
Interface to make sure you can find the correct ClusterItem
object held by the renderer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With