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

API 33下Google Map添加新Marker函数未调用,API 31正常

API 33下Google Maps标记添加函数未执行的排查与解决建议

问题场景

在Google Maps功能开发中,updateLocationOnMap方法负责移除旧标记并调用placeMarkerOnMap添加新标记,该逻辑在API 31设备上正常运行,但在API 33模拟器中,placeMarkerOnMap完全未被执行(断点到调用行无触发),且旧标记移除逻辑正常执行。

排查与解决建议

1. 检查代码是否因未捕获异常中断

在updateLocationOnMap的后续代码块添加异常捕获,确认是否有隐性异常导致代码终止:

fun updateLocationOnMap(location: String, address: Address) {
        val latLng = LatLng(address.latitude, address.longitude)
        locationPanelBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED

        if (mCurrLocationMarker != null) {
            mCurrLocationMarker!!.remove()
        }
        try {
            placeMarkerOnMap(latLng, location)
            mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng))
        } catch (e: Exception) {
            e.printStackTrace() // 查看Logcat输出的异常信息定位问题
        }
}

2. 确保地图操作在主线程执行

Google Maps的UI操作必须在主线程完成,API 33对线程合规性检查更严格,可强制将标记添加逻辑切换到主线程:

fun updateLocationOnMap(location: String, address: Address) {
        val latLng = LatLng(address.latitude, address.longitude)
        locationPanelBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED

        if (mCurrLocationMarker != null) {
            mCurrLocationMarker!!.remove()
        }
        // 切换到主线程执行地图操作
        runOnUiThread {
            placeMarkerOnMap(latLng, location)
            mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng))
        }
}

3. 升级Google Maps SDK版本适配API 33

旧版本的play-services-maps可能存在API 33兼容性问题,升级到最新稳定版本(如21.0.1及以上):
在Module级别build.gradle中更新依赖:

dependencies {
    implementation 'com.google.android.gms:play-services-maps:21.0.1'
}

4. 确认地图实例mMap已初始化完成

API 33中地图初始化时机可能存在差异,确保调用updateLocationOnMap时mMap已完成初始化(即onMapReady回调已执行),添加空安全判断:

fun updateLocationOnMap(location: String, address: Address) {
        val latLng = LatLng(address.latitude, address.longitude)
        locationPanelBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED

        if (mCurrLocationMarker != null) {
            mCurrLocationMarker!!.remove()
        }
        mMap?.let { map ->
            placeMarkerOnMap(latLng, location)
            map.animateCamera(CameraUpdateFactory.newLatLng(latLng))
        }
}

5. 排查BottomSheet状态变更的阻塞影响

尝试延后locationPanelBehavior的状态设置逻辑,避免其阻塞后续代码执行:

fun updateLocationOnMap(location: String, address: Address) {
        val latLng = LatLng(address.latitude, address.longitude)

        if (mCurrLocationMarker != null) {
            mCurrLocationMarker!!.remove()
        }
        placeMarkerOnMap(latLng, location)
        mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng))
        
        // 延后设置BottomSheet状态,避免阻塞地图操作
        locationPanelBehavior?.post {
            locationPanelBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED
        }
}

内容的提问来源于stack exchange,提问作者CharmingCauliflower

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 13:57:33