1

I'm trying to add ImageVector icons I have (created as, for example, Icons.Default.Home) to Markers in Google Maps. What I currently have is this:

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.

What I found out it that BitmapDescriptorFactory can create the requested BitmapDescriptor by passing a resource or a bitmap (respectively BitmapDescriptorFactory.fromResource(resourceId: Int) and BitmapDescriptorFactory.fromBitmap(image: Bitmap)).

The problem is I cannot find out how to convert the ImageVector I got from Icons.Default.Home to any of the above. I found some questions on the Stack about converting a vector drawable to a bitmap, but still, none of them covers the problem with converting ImageVector. I also checked if the instance of ImageVector has any potentially helpful methods, but it can only - if I'm not mistaken - convert it to a String, which is totally useless in my case.

Any help will be highly appreciated

Amvix
  • 213
  • 4
  • 12

1 Answers1

1

Ok, I think I somehow made it with a (not so) dirty trick. I downloaded the icons I needed from Google Material Icons site (https://fonts.google.com/icons?selected=Material+Icons) and imported them into my project. Later I used a snippet provided by @Leo Droidcoder here: Custom marker in google maps in android with vector asset icon to use these icons as markers.

This way I have markers that look like the icons I wanted, but I believe there must be a better solution.

Amvix
  • 213
  • 4
  • 12
  • As of April 2023, I don't think there is. I searched the Compose UI sources, and it appears the only thing capable of rendering an ImageVector is the VectorPainter, but that is very explicitly internal and can only be used to render things in a composition, not imperatively?? This looks like a very arbitrary limitation, or, more likely, nobody bothered to expose the functionality for non-composition uses. – matejcik Apr 11 '23 at 10:30