Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the clicked marker icon using android-maps-utils?

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?


Related

  • How to highlight the selected cluster item?
  • Android Google Map V2: How to change previous clicked marker's icon when clicked on another marker
like image 457
JJD Avatar asked Jun 15 '15 10:06

JJD


People also ask

How do I change the marker on Google Maps Android?

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.

How do I change the color of a marker on Google Maps?

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.


1 Answers

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));
    }
}
like image 194
JJD Avatar answered Oct 20 '22 16:10

JJD