有没有办法更改 Compose 中 Google Maps API 按钮的位置?

问题描述 投票:0回答:1

我想移动地图按钮,主要是指南针按钮(出现在左上角)和我的位置按钮(出现在右上角),以便我可以将它们放下或放在除所有位置之外的任何位置通往山顶的路。
目前,它们使用起来很复杂,因为它们位于状态栏和我的相机位置下方,但我喜欢地图一直延伸到顶部,而且我通常不希望将地图向下移动。
我的地图的屏幕截图
这是我的代码:

@Composable
fun MapsScreen() {
    // Obtener el contexto actual de la aplicación
    val context = LocalContext.current

    // Verificar si el dispositivo está en tema oscuro o claro
    val isDarkTheme =
        when (context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
            Configuration.UI_MODE_NIGHT_YES -> true
            else -> false
        }

    // Elegir el estilo del mapa según el tema del dispositivo
    val mapStyleTheme = if (isDarkTheme) {
        R.raw.mapstyle_night
    } else {
        R.raw.mapstyle_day
    }

    // Obtener el cliente FusedLocationProviderClient para obtener la última ubicación conocida
    val fusedLocationClient: FusedLocationProviderClient =
        LocationServices.getFusedLocationProviderClient(context)
    var lastKnownLocation by remember {
        mutableStateOf(
            LatLng(
                0.0,
                0.0
            )
        )
    }
    var zoom by remember { mutableFloatStateOf(10f) }

    // Comprobación de permisos requerida (obligatoria) por de Android
    if (ActivityCompat.checkSelfPermission(
            context,
            Manifest.permission.ACCESS_FINE_LOCATION
        ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
            context,
            Manifest.permission.ACCESS_COARSE_LOCATION
        ) != PackageManager.PERMISSION_GRANTED
    ) {
        return
    }

    // Mover la cámara a la última ubicación conocida
    val cameraPositionState = rememberCameraPositionState {
        fusedLocationClient.lastLocation
            .addOnSuccessListener { location: Location? ->
                // Verifica si la ubicación no es nula
                location?.let {
                    // Actualiza la latitud y longitud
                    val latitud = location.latitude
                    val longitud = location.longitude
                    // Actualiza la última ubicación conocida
                    lastKnownLocation = LatLng(latitud, longitud)
                    println("Location: $lastKnownLocation")
                    zoom = 15f
                    // Actualiza la posición de la cámara
                    position = CameraPosition.fromLatLngZoom(lastKnownLocation, zoom)
                }
            }
    }
    // Configuración de la UI del mapa
    val uiSettings by remember {
        mutableStateOf(
            MapUiSettings(
                myLocationButtonEnabled = true,
                mapToolbarEnabled = true,
            )
        )
    }
    // Configuración de las propiedades del mapa
    val properties by remember {
        mutableStateOf(
            MapProperties(
                mapType = MapType.NORMAL,
                isMyLocationEnabled = true,
                mapStyleOptions = MapStyleOptions.loadRawResourceStyle(
                    context,
                    mapStyleTheme
                ) // Estilo del mapa personalizado basado en el tema del dispositivo (mapStyleTheme)
            )
        )
    }
    // Composable para mostrar el mapa
    GoogleMap(
        modifier = Modifier.fillMaxSize(),
        cameraPositionState = cameraPositionState,
        properties = properties,
        uiSettings = uiSettings
    ) {
        Marker(
            state = MarkerState(position = lastKnownLocation),
            title = "lastKnownLocation",
            snippet = "Marker in lastKnownLocation"
        )
    }
}

目前我还没有找到任何文档能够改变Android中按钮的位置。

android google-maps android-jetpack-compose google-maps-android-api-2 google-maps-android-api-3
1个回答
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.