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

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

Posted by: admin June 15, 2020 Leave a comment

Questions:

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&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));
    }
}

Answer:

Thanks to @JJD, I wrote the same in Kotlin.

private var selectedBitmap: BitmapDescriptor? = null
private var unselectedBitmap: BitmapDescriptor? = null
private var lastMarker: Marker? = null
private var clusterManager: ClusterManager<StationClusterItem>? = null

override fun onMapReady(googleMap: GoogleMap) {
    this.googleMap = googleMap

    clusterManager = ClusterManager(context!!, googleMap)
    val clusterRenderer = MarkerClusterRenderer(context!!, googleMap, clusterManager!!,
        unselectedBitmap!!)
    clusterManager!!.renderer = clusterRenderer
    // Add your items to the ClusterManager here with clusterManager?.addItem().
    // Better is in background thread.
    val boundsBuilder = LatLngBounds.Builder()
    ...

    clusterManager!!.cluster()
    // Add this listener to make ClusterManager correctly zoom clusters and markers.
    googleMap.setOnCameraIdleListener(clusterManager)

    // This method is needed to use setOnClusterItemClickListener.
    googleMap.setOnMarkerClickListener(clusterManager)
    clusterManager!!.setOnClusterItemClickListener { item ->
        lastMarker?.setIcon(unselectedBitmap)
        lastMarker = clusterRenderer.getMarker(item) // Here we access a selected marker.
        lastMarker?.setIcon(selectedBitmap)
        // false - to show info window. See GoogleMap.InfoWindowAdapter.
        // true - to hide info window.
        false
    }
}

class MarkerClusterRenderer(
    val context: Context,
    val map: GoogleMap,
    clusterManager: ClusterManager<StationClusterItem>,
    private val markerBitmap: BitmapDescriptor
) : DefaultClusterRenderer<StationClusterItem>(context, map, clusterManager) {

    init {
        minClusterSize = 1 // 2, 3 or more, as you wish.
    }    

    override fun onBeforeClusterItemRendered(item: StationClusterItem?,
                                             markerOptions: MarkerOptions?) {
        super.onBeforeClusterItemRendered(item, markerOptions)
        markerOptions?.icon(markerBitmap)
    }
}

UPDATE

I got an exception: java.lang.IllegalArgumentException: Unmanaged descriptor when clicked markers (in a line lastMarker?.setIcon(unselectedBitmap)).

After studying IllegalArgumentException: Unmanaged descriptor using gms.maps.model.Marker.setIcon I rewrote that listener:

private var selectedItem: StationClusterItem? = null

    clusterManager!!.setOnClusterItemClickListener { item ->
        if (selectedItem != null) {
            // Set here a reference to a previous marker.
            // We save a reference to a previous item, not to a marker.
            val lastMarker = clusterRenderer.getMarker(selectedItem)
            lastMarker?.setIcon(unselectedBitmap)
        }
        selectedItem = item
        // Now get a reference to a selected marker.
        val newMarker = clusterRenderer.getMarker(item)
        newMarker?.setIcon(selectedBitmap)
        false
    }