我想移动地图按钮,主要是指南针按钮(出现在左上角)和我的位置按钮(出现在右上角),以便我可以将它们放下或放在除所有位置之外的任何位置通往山顶的路。
目前,它们使用起来很复杂,因为它们位于状态栏和我的相机位置下方,但我喜欢地图一直延伸到顶部,而且我通常不希望将地图向下移动。
我的地图的屏幕截图
这是我的代码:
@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 Maps Compose 构建在 Android 版 Maps SDK 之上。 指南针在 SDK 中无法定位。 请参阅 https://developers.google.com/maps/documentation/android-sdk/controls#compass 和 https://developers.google.com/android/reference/com/google/android/gms/maps/ GoogleMapOptions.
这里是在 Android Maps Compose 库中设置指南针启用的位置 https://github.com/googlemaps/android-maps-compose/blob/d5533b47f407414ef27c47eefd69844c033b337a/maps-compose/src/main/java/com/google/maps/android/compose/MapUpdater.kt#L151