0

I am using MapBox 8.4.0 and I have the following snippet to load the map on a fragment, pinning the user's current location with a marker. I need to customize the marker by dynamically setting foregroundDrawable with an image loaded from a network URL. But foregroundDrawable only accepts a resource ID as parameter.

val customOptions = LocationComponentOptions.builder(context!!)
    .elevation(5f)
    .foregroundDrawable(R.drawable.icon_profile) // set image dynamically
    .backgroundDrawable(R.drawable.icon_current_location)
    .build()

val activationOptions = LocationComponentActivationOptions.builder(context!!, style)
    .locationComponentOptions(customOptions)
    .build()

mapboxMap.locationComponent.apply {
    activateLocationComponent(activationOptions)
    isLocationComponentEnabled = true
    cameraMode = CameraMode.TRACKING
    renderMode = RenderMode.NORMAL
}

It should look like this with the profile icon replaced with the loaded image at run time.

https://i.stack.imgur.com/eoXuG.jpg

Any way I could achieve this?

zeenosaur
  • 888
  • 11
  • 16

2 Answers2

1

We can use foregroundName() to set dynamic icon for our marker.

mapboxMap.getStyle { loadedStyle ->

    loadedStyle.addImage("marker-icon", bitmapIcon) // create a Bitmap icon; you may use Glide to load image from URL

    val locationComponentOptions: LocationComponentOptions =
        LocationComponentOptions.builder(context!!)
            .foregroundName("marker-icon") // set icon for the marker
            .build()

    val activationOptions =
        LocationComponentActivationOptions.builder(context!!, loadedStyle)
            .locationComponentOptions(locationComponentOptions)
            .build()

    mapboxMap.locationComponent.apply {
        activateLocationComponent(activationOptions)
        ...
    }
}
zeenosaur
  • 888
  • 11
  • 16
0

Fly by comment here to say that if you're using Picasso instead of Glide, https://stackoverflow.com/a/20181629/6358488 shows how to use Picasso to set a target to get the Bitmap from the network URL call.

langsmith
  • 2,529
  • 1
  • 11
  • 13