简体   繁体   中英

Render cluster icon from url using google maps utils

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.

在此处输入图片说明

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM