In my Android project I am using the android-maps-utils library to apply clustering to a bunch of markers on a map view. Whenever a marker is clicked I get notified via onClusterItemClick
so I can do some action.
public interface OnClusterItemClickListener<T extends ClusterItem> {
public boolean onClusterItemClick(T item);
}
Now I would like to let the user know which marker has been clicked. The easies visual feedback would be to change the (color of the) marker icon. A icon can be set via the MarkerOptions
object which can be access within onBeforeClusterItemRendered(T item, MarkerOptions markerOptions)
such as here:
markerOptions.icon(
BitmapDescriptorFactory.defaultMarker(
BitmapDescriptorFactory.HUE_YELLOW));
If I would have access to the Marker
object itself such as in onMarkerClick (Marker marker)
I could change the icon via setIcon
.
How can I change the clicked marker icon?
For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.
To edit the marker color, click or tap on the marker icon. When you do that, you can change both the color of the marker and its style. Go for the color or style you want to change and then click OK to see the effect. Click on the button labeled Done to save your new marker color settings.
I noticed that the DefaultClusterRenderer
provides methods to retrieve the Marker
object associated with a ClusterItem
. Since I use a custom renderer anyways I was able to access the desired Marker
object as shown here:
mSelectedMarker = mCustomClusterItemRenderer.getMarker(mSelectedClusterItem);
This allows me to change the icon within onClusterItemClick()
:
private void updateSelectedMarker() {
if (mSelectedMarker != null) {
mSelectedMarker.setIcon(
BitmapDescriptorFactory.defaultMarker(
BitmapDescriptorFactory.HUE_YELLOW));
}
}
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