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

Jetpack Compose中Google Maps的MyLocationButton不显示问题求助

解决Jetpack Compose中Google Maps MyLocationButton不显示的问题

你的代码里已经开启了myLocationButtonEnabled,但要让定位按钮正常显示,还需要满足几个关键条件,以下是具体修复步骤:

1. 启用我的位置图层

GoogleMap组件必须开启myLocationEnabled参数,这是显示定位按钮的必要前提。修改你的GoogleMap调用代码:

GoogleMap(
    modifier = Modifier.fillMaxSize(),
    cameraPositionState = cameraPositionState,
    uiSettings = uiSettings,
    myLocationEnabled = true, // 新增此参数
    onMyLocationButtonClick = {
        true
    }
) {
    Marker(
        state = MarkerState(position = singapore),
        title = "Singapore",
        snippet = "Marker in Singapore"
    )
}

2. 申请位置权限

定位功能需要系统位置权限支持,你需要完成以下操作:

  • 在AndroidManifest.xml中添加权限声明:
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- 若不需要高精度定位,可替换为ACCESS_COARSE_LOCATION -->
    
  • 在Compose中动态申请权限,示例代码:
    val locationPermissionLauncher = rememberLauncherForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { isGranted ->
        if (isGranted) {
            // 权限授予后,定位功能可正常使用
        }
    }
    
    LaunchedEffect(Unit) {
        locationPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
    }
    

3. 其他必要检查

  • 确认Google Maps API密钥已正确配置在AndroidManifest.xml中,且在Google Cloud控制台启用了Maps SDK for Android服务。
  • 检查maps-compose依赖版本是否为最新稳定版,避免因版本bug导致功能异常。

完成上述修改后重新运行项目,MyLocationButton应该就能正常显示了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 13:01:10