0

I would like to move the map buttons, mainly the Compass button (which appears at the top left) and the My Location button (which appears at the top right) so that I can put them down or in any position other than all the way to the top.
Currently they are complicated to use as they are below the Status Bar and my camera position, but I like that the map extends all the way to the top and I would not like to generally move the map down.
Screenshot with my Map
This is my code:

@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"
        )
    }
}

At the moment I have not found any documentation to be able to change the position of the buttons in Android.

2 Answers 2

0

The Android Maps Compose is built on top of the Maps SDK for Android. The compass is not positionable in the SDK. See https://developers.google.com/maps/documentation/android-sdk/controls#compass and https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMapOptions.

Here is where the compass enablement is set in the Android Maps Compose library https://github.com/googlemaps/android-maps-compose/blob/d5533b47f407414ef27c47eefd69844c033b337a/maps-compose/src/main/java/com/google/maps/android/compose/MapUpdater.kt#L151

0

Use the contentPadding argument of GoogleMap(). This will move your Map buttons.

GoogleMap(contentPadding = PaddingValues(top = 64.dp))