You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

android-maps-utils v1.2.1中DefaultClusterRenderer重写编译失败咨询

问题原因与修复方案

从android-maps-utils v1.1.0升级到v1.2.1后,你遇到的编译问题主要源于库对可空类型的约束收紧以及部分方法签名的变更,具体细节和修复方案如下:

版本间核心变更点

  • 泛型参数可空性限制:v1.2.x版本开始,DefaultClusterRenderer和ClusterManager的泛型类型不再支持可空类型(即<ClusterStation?>不再合法),库内部对ClusterItem的处理默认假设为非空实例。
  • 方法参数类型调整:所有重写方法(如onBeforeClusterItemRendered、onClusterItemRendered)的参数从可空类型改为非空类型,原来的ClusterStation?、MarkerOptions?等参数现在要求为非空。
  • 构造函数参数约束:ClusterManager的泛型参数同样不再允许可空,构造函数传入的实例类型需要匹配非空泛型。

具体修复步骤

  1. 修正泛型类型:将CustomClusterRenderer和构造函数中的ClusterManager泛型从ClusterStation?改为ClusterStation。
  2. 调整重写方法的参数类型:移除参数的可空标记(?),并根据非空特性简化代码中的空安全调用。
  3. 确保ClusterStation实例非空:在使用ClusterManager添加集群项时,确保传入的ClusterStation都是非空实例,避免空指针问题。

修改后的完整代码

package no.rogo.emptyfuel.utilities.cluster

import android.content.Context
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
import com.google.maps.android.clustering.Cluster
import com.google.maps.android.clustering.ClusterManager
import com.google.maps.android.clustering.view.DefaultClusterRenderer
import com.google.maps.android.ui.IconGenerator
import no.rogo.emptyfuel.utilities.StatusLevel

/**
 * Created by Roar on 05.04.2020.
 * Copyright RoGo Software / Gronmo IT
 */
class CustomClusterRenderer(
    context: Context,
    map: GoogleMap,
    clusterManager: ClusterManager<ClusterStation>
) : DefaultClusterRenderer<ClusterStation>(context, map, clusterManager) {
    private val TAG by lazy { this::class.java.simpleName }
    private val clusterIconGenerator = IconGenerator(context.applicationContext)

    override fun onBeforeClusterItemRendered(
        item: ClusterStation,
        markerOptions: MarkerOptions
    ) {
        val markerHue = when(item.statusLevel) {
            StatusLevel.SINCE_NEW -> BitmapDescriptorFactory.HUE_GREEN
            StatusLevel.SINCE_FAIR -> BitmapDescriptorFactory.HUE_YELLOW
            StatusLevel.SINCE_OLD -> BitmapDescriptorFactory.HUE_RED
            StatusLevel.SINCE_OLDER -> BitmapDescriptorFactory.HUE_BLUE
            StatusLevel.NOT_SET -> BitmapDescriptorFactory.HUE_CYAN
            StatusLevel.NOT_AVAILABLE -> BitmapDescriptorFactory.HUE_AZURE
            StatusLevel.HIDDEN -> BitmapDescriptorFactory.HUE_ROSE
            StatusLevel.UNCERTAIN -> BitmapDescriptorFactory.HUE_ORANGE
            StatusLevel.UNKNOWN -> BitmapDescriptorFactory.HUE_MAGENTA
            else -> BitmapDescriptorFactory.HUE_VIOLET
        }
        val markerDescriptor = BitmapDescriptorFactory.defaultMarker(markerHue)
        markerOptions.icon(markerDescriptor)
        super.onBeforeClusterItemRendered(item, markerOptions)
    }

    override fun onClusterItemRendered(clusterItem: ClusterStation, marker: Marker) {
        super.onClusterItemRendered(clusterItem, marker)
    }

    override fun onBeforeClusterRendered(
        cluster: Cluster<ClusterStation>,
        markerOptions: MarkerOptions
    ) {
        super.onBeforeClusterRendered(cluster, markerOptions)
    }
}

额外注意事项

  • 如果你的ClusterStation类中statusLevel存在可能为空的情况,建议在when表达式中添加空值分支处理,或者确保statusLevel始终有默认值。
  • 升级后建议同步检查ClusterManager的使用代码,确保添加的集群项都是非空的ClusterStation实例,比如如果之前有addItem(null)的调用,需要改为合法的非空实例。

内容的提问来源于stack exchange,提问作者Roar Grønmo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 15:42:38