动态设置 LocationComponentOptions foregroundDrawable

Set LocationComponentOptions foregroundDrawable dynamically

我正在使用 MapBox 8.4.0 并且我有以下代码片段将地图加载到片段上,用标记固定用户的当前位置。我需要通过使用从网络 URL 加载的图像动态设置 foregroundDrawable 来自定义标记。但是 foregroundDrawable 只接受资源 ID 作为参数。

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
}

在 运行 时将 profile icon 替换为加载的图像后,它应该看起来像这样。

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

我有什么方法可以做到这一点?

我们可以使用foregroundName()为我们的标记设置动态图标。

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)
        ...
    }
}

请在此处发表评论,说明如果您使用的是 Picasso 而不是 Glide, 展示了如何使用 Picasso 设置目标以从网络 Bitmap 获取 [=14] =] 呼叫.