2

I use dynamic icon for each cluster item , so I have special icon url and load marker icon from url . I user following code :

override fun onBeforeClusterItemRendered(item: T, markerOptions: MarkerOptions?) {

    super.onBeforeClusterItemRendered(item, markerOptions)
try {
    var url = URL("https://cdn3.iconfinder.com/data/icons/places/100/map_pin_big_1-128.png")
    Glide.with(context)
        .asBitmap()
        .load(url)
        .into(object : CustomTarget<Bitmap>() {
            override fun onLoadCleared(placeholder: Drawable?) {
            }

            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                markerOptions?.icon(BitmapDescriptorFactory.fromBitmap(resource))
            }

        })

} catch (ex: Exception) {
    Log.e("map", ex.toString())
}

}

some icons still default in my case, after zoom in zoom out icon changes sometimes. The problem is that this code is not works for every cluster item , after zoom changed cluster icon is changed too, it may render my custom icon and may use default.

enter image description here

enter image description here

Zoe
  • 27,060
  • 21
  • 118
  • 148
Nininea
  • 2,671
  • 6
  • 31
  • 57
  • 1
    Please don't use the glide tag for questions about the Android image loading library - use [android-glide] instead. – Zoe Apr 24 '19 at 13:05

1 Answers1

6

You just need to make all this stuff in

protected void onClusterItemRendered(T clusterItem, Marker marker) {
...
}

In onBeforeClusterItemRendered you set icon on MarkerOptions in async callback. At this time it could be added to map and become real Marker. So you icon will be set to already useless object.

That's why you need to do it in onClusterItemRendered

only difference to change:

markerOptions?.icon(BitmapDescriptorFactory.fromBitmap(resource))

to:

marker.setIcon(BitmapDescriptorFactory.fromBitmap(resource))

Reference: Refreshing makers (ClusterItems) in Google Maps v2 for Android

Thanks to https://stackoverflow.com/users/3252320/stas-parshin

Shehryar Khan
  • 71
  • 1
  • 4