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

如何让Android Auto中Google Map内容避开UI遮挡居中显示?

Android Auto地图当前位置被UI遮挡的解决方法

问题核心

Android Auto界面存在系统UI覆盖层(如应用信息栏),直接调用googleMap.moveCamera()将当前位置居中到屏幕中心时,会被这些遮挡层覆盖。我们需要利用Android Auto提供的**稳定区域(Stable Area)**来确保地图内容显示在无遮挡的安全区域内。

解决方案

1. 基于Stable Area调整相机位置

Stable Area是系统保证不会被UI遮挡的区域,我们需要将地图的焦点定位到这个区域的中心,而非整个屏幕中心。

2. 修改核心逻辑

以下是修改后的关键代码部分:

完善moveCurrentLocationToStableAreaCenter方法

private fun moveCurrentLocationToStableAreaCenter(currentLocation: LatLng) {
    val googleMap = googleMap ?: return
    val stableArea = mStableArea ?: return
    val projection = googleMap.projection ?: return

    // 将当前位置转换为屏幕坐标
    val locationScreenPoint = projection.toScreenLocation(currentLocation)
    // 计算Stable Area的中心坐标
    val stableCenterPoint = Point(stableArea.centerX(), stableArea.centerY())

    // 计算偏移量,将当前位置移动到Stable Area中心
    val offsetX = stableCenterPoint.x - locationScreenPoint.x
    val offsetY = stableCenterPoint.y - locationScreenPoint.y
    val adjustedScreenPoint = Point(locationScreenPoint.x + offsetX, locationScreenPoint.y + offsetY)

    // 将调整后的屏幕坐标转回经纬度
    val adjustedLatLng = projection.fromScreenLocation(adjustedScreenPoint)
    adjustedLatLng?.let {
        val cameraUpdate = CameraUpdateFactory.newLatLngZoom(it, googleMap.cameraPosition.zoom)
        googleMap.moveCamera(cameraUpdate)
    }
}

更新locationUpdated方法

替换原来直接居中的逻辑,改为调用上面的方法:

override fun locationUpdated(location: Location) {
    Log.d(TAG, "Getting current location $location")
    val currentLatLng = LatLng(location.latitude, location.longitude)
    // 使用Stable Area中心定位,避免遮挡
    moveCurrentLocationToStableAreaCenter(currentLatLng)
}

处理区域变化回调

在onStableAreaChanged中触发重新定位,确保UI变化时地图内容始终在安全区域:

override fun onStableAreaChanged(stableArea: Rect) {
    synchronized(this@SurfaceRenderer) {
        Log.i(TAG, "Stable area changed: $stableArea")
        mStableArea = stableArea
        // 获取当前位置(如果有)并重新定位到Stable Area中心
        googleMap?.myLocation?.let { location ->
            moveCurrentLocationToStableAreaCenter(LatLng(location.latitude, location.longitude))
        }
        renderFrame()
    }
}

修正代码笔误

原代码中this@SurfaceRendererNew是笔误,统一改为this@SurfaceRenderer。

额外注意事项

  • 确保在地图初始化完成后再进行相机调整,避免googleMap为空
  • 保留原有的权限检查逻辑,确保位置权限已授予
  • 当Stable Area为空时,可以降级使用Visible Area作为备选

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 07:45:14