如何在 Jetpack Compose 中将图像 url 加载到谷歌地图标记图标

发布于 2025-01-10 22:18:27 字数 54 浏览 2 评论 0原文

我从服务器获取图像 URL,并希望在 Google 地图标记图标中加载该 URL。我该怎么做?

I get the image URL from the server and I want to load the URL in the Google Maps marker icon. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想挽留 2025-01-17 22:18:27

使用 Glide 库

fun loadIcon(
    context: Context,
    url: String?,
    placeHolder: Int,
): BitmapDescriptor? {
    try {
        var bitmap: Bitmap? = null
        Glide.with(context)
            .asBitmap()
            .load(url)
            .error(placeHolder)
            // to show a default icon in case of any errors
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(
                    resource: Bitmap,
                    transition: Transition<in Bitmap>?
                ) {

                    bitmap = resource

                }

                override fun onLoadCleared(placeholder: Drawable?) {

                }
            })
        return BitmapDescriptorFactory.fromBitmap(bitmap!!)
    } catch (e: Exception) {
        e.printStackTrace()
        return null
    }

}

然后调用

 var bitmap = loadIcon(context, item.icon, R.drawable.placeholder_image,)
                    Marker(
                        state = MarkerState(
                            position = LatLng(
                                item.lat.toDouble(),
                                item.long.toDouble()
                            )
                        ),
                        title = item.name,
                        icon = bitmap,
                        )

using Glide Library

fun loadIcon(
    context: Context,
    url: String?,
    placeHolder: Int,
): BitmapDescriptor? {
    try {
        var bitmap: Bitmap? = null
        Glide.with(context)
            .asBitmap()
            .load(url)
            .error(placeHolder)
            // to show a default icon in case of any errors
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(
                    resource: Bitmap,
                    transition: Transition<in Bitmap>?
                ) {

                    bitmap = resource

                }

                override fun onLoadCleared(placeholder: Drawable?) {

                }
            })
        return BitmapDescriptorFactory.fromBitmap(bitmap!!)
    } catch (e: Exception) {
        e.printStackTrace()
        return null
    }

}

then call

 var bitmap = loadIcon(context, item.icon, R.drawable.placeholder_image,)
                    Marker(
                        state = MarkerState(
                            position = LatLng(
                                item.lat.toDouble(),
                                item.long.toDouble()
                            )
                        ),
                        title = item.name,
                        icon = bitmap,
                        )
梅倚清风 2025-01-17 22:18:27

我写了一篇文章在谷歌地图上显示自定义标记:

https://towardsdev.com/jetpack-compose-custom-google-map-marker-erselan-khan-e6e04178a30b

您必须从图像网址中获取可绘制对象,您可以轻松获取该对象使用线圈库进行图像加载。

https://towardsdev.com/image-url- to-bitmap-using-coil-erselan-khan-6f190ce83d7f

I have written an article to show custom marker on google map:

https://towardsdev.com/jetpack-compose-custom-google-map-marker-erselan-khan-e6e04178a30b

You have to get the drawable object from your image url, which you can easily get by using the coil library for image loading.

https://towardsdev.com/image-url-to-bitmap-using-coil-erselan-khan-6f190ce83d7f

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文