In our project we use google maps v2, and I need to find a way to change pin icon after the pin was tapped. Also I need to return initial icon for this pin when another pin will
So, I found solution for it - maybe this also will helps to someone.
So, at first, we need to use our custom renderer (inherited from DefaultClusterRenderer
). DefaultClusterRenderer
has his own cache that contains pairs of ClusterItem
and corresponding Marker
:
public MarkerCache mMarkerCache = new MarkerCache();
so I wrote the next method in our CustomClusterRenderer
public Marker getMarker(OurClusterItem clusterItem) {
return mMarkerCache.get(clusterItem);
}
After it, I've added to our fragment 2 variables:
private Marker mCurrentSelectedMarker;
private ClusterStore mCurrentSelectedClusterItem;
and change implementation for ClusterItemClickListener
:
public ClusterManager.OnClusterItemClickListener mClusterItemClickListener = new ClusterManager.OnClusterItemClickListener() {
@Override
public boolean onClusterItemClick(ClusterStore item) {
// return to previous marker non-selected icon
if (mCurrentSelectedMarker != null) {
mCurrentSelectedMarker.setIcon(BitmapDescriptorFactory.fromResource(mCurrentSelectedClusterItem.getIconResourceId()));
}
Marker marker = mCustomRenderer.getMarker(item);
if (marker != null) {
mCurrentSelectedMarker = marker;
mCurrentSelectedClusterItem = item;
marker.setIcon(BitmapDescriptorFactory.fromResource(item.getIconSelResourceId()));
}
// some other code
return true;
}
};
That's all, and it works like a charm.