【问题标题】:Converting ImageVector from Jetpack Compose to BitmapDescriptor将 ImageVector 从 Jetpack Compose 转换为 BitmapDescriptor
【发布时间】:2021-08-12 09:04:25
【问题描述】:

我正在尝试将我拥有的 ImageVector 图标(例如,创建为 Icons.Default.Home)添加到 Google 地图中的标记。我目前拥有的是这样的:

val opt = MarkerOptions()
opt.position(LatLng(lat, lng))          // lat and lng are defined above
opt.title(title)                        // title as well
opt.icon(BitmapDescriptorFactory.?????) // and there the problem occurs.

我发现BitmapDescriptorFactory 可以通过传递资源或位图(分别为BitmapDescriptorFactory.fromResource(resourceId: Int)BitmapDescriptorFactory.fromBitmap(image: Bitmap))来创建请求的BitmapDescriptor

问题是我不知道如何将我从Icons.Default.Home 获得的 ImageVector 转换为上述任何一种。我在堆栈上发现了一些关于将可绘制矢量转换为位图的问题,但仍然没有一个涵盖转换 ImageVector 的问题。我还检查了 ImageVector 的实例是否有任何可能有用的方法,但它只能 - 如果我没记错的话 - 将其转换为字符串,这在我的情况下完全没用。

我们将不胜感激任何帮助

【问题讨论】:

    标签: android google-maps android-jetpack android-jetpack-compose


    【解决方案1】:

    好吧,我想我用了一个(不是那么)肮脏的把戏。 我从 Google Material Icons 网站 (https://fonts.google.com/icons?selected=Material+Icons) 下载了我需要的图标并将它们导入到我的项目中。后来我在这里使用了@Leo Droidcoder 提供的一个sn-p:Custom marker in google maps in android with vector asset icon 来使用这些图标作为标记。

    这样我的标记看起来就像我想要的图标,但我相信一定有更好的解决方案。

    【讨论】:

      猜你喜欢
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2022-11-16
      • 2022-08-11
      • 2022-10-23
      • 1970-01-01
      • 2022-11-23
      相关资源
      最近更新 更多