简体   繁体   English

使用谷歌地图工具从 url 渲染集群图标

[英]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 .我为每个集群项目使用动态图标,所以我有特殊的图标 url 和从 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.在 onBeforeClusterItemRendered 中,您在异步回调中的 MarkerOptions 上设置图标。 At this time it could be added to map and become real Marker.这时候就可以添加到地图上,成为真正的Marker了。 So you icon will be set to already useless object.所以你的图标将被设置为已经无用的对象。

That's why you need to do it in onClusterItemRendered这就是为什么您需要在 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参考: Android 版 Google Maps v2 中的刷新制造商 (ClusterItems)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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