Can someone explain how to change marker icon with kotlin

Been trying to change marker icon on the map with kotlin. but it always not able to use the Drawable resource. always says that it is expecting ICON but I gave drawable !! and I have no other choice than sending Drawable to it !!

If you have a typical image file (png for example) in your drawable folder, you can use Icon.Factory.fromResources function:

tomtomMap.addMarker(MarkerBuilder(latLng)
    .icon(Icon.Factory.fromResources(this, R.drawable.pin)))

If your image is defined as an android vector drawable, you can create a BitmapDrawable from it and use it inside Icon.Factory.fromDrawable function:

val drawable = this.resources.getDrawable(R.drawable.marker, theme)

val bitmap = Bitmap.createBitmap(
    drawable.intrinsicWidth,
    drawable.intrinsicHeight,
    Bitmap.Config.ARGB_8888
)

val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)

val bitmapDrawable = BitmapDrawable(this.resources, bitmap)

tomtomMap.addMarker(MarkerBuilder(latLng)
    .icon(Icon.Factory.fromDrawable("name", bitmapDrawable)))
1 Like