Render cluster icon from url using google maps utils

877 views Asked by At

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

1

There are 1 answers

1
Shehryar Khan On

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