Get Location App

This commit is contained in:
Ari
2025-10-23 20:08:35 +02:00
parent 8603b1b739
commit 962dff81ec
4 changed files with 75 additions and 9 deletions
@@ -18,6 +18,7 @@ import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@@ -40,6 +41,7 @@ import com.google.android.gms.location.LocationServices
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlinx.coroutines.launch
import androidx.compose.runtime.collectAsState
class MainActivity : ComponentActivity() {
@@ -58,6 +60,8 @@ class MainActivity : ComponentActivity() {
)
}
val locationDataStore = remember { LocationDataStore(context) }
val locationPermissions = rememberMultiplePermissionsState(
permissions = listOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
@@ -65,9 +69,9 @@ class MainActivity : ComponentActivity() {
)
)
var location by remember {
mutableStateOf<Location?>(null)
}
var location by remember { mutableStateOf<Location?>(null) }
val savedLocation by locationDataStore.locationFlow.collectAsState(initial = null)
val coroutineScope = rememberCoroutineScope()
@@ -76,10 +80,17 @@ class MainActivity : ComponentActivity() {
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
location?.let {
Text(text ="${it.latitude} ${it.longitude}",
fontSize = 32.sp,
fontWeight = FontWeight.Bold
// Zeige aktuelle oder gespeicherte Location
val displayLocation = location ?: savedLocation
displayLocation?.let {
Text(
text = when {
location != null -> "Aktuelle Location:\n${location!!.latitude} ${location!!.longitude}"
savedLocation != null -> "Gespeicherte Location:\n${savedLocation!!.latitude} ${savedLocation!!.longitude}"
else -> "Keine Location verfügbar"
},
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
}
Button(
@@ -88,12 +99,17 @@ class MainActivity : ComponentActivity() {
locationPermissions.launchMultiplePermissionRequest()
} else {
coroutineScope.launch {
location = locationManager.getLocation()
val newLocation = locationManager.getLocation()
location = newLocation
// Speichere Location auf Disk
newLocation?.let {
locationDataStore.saveLocation(it.latitude, it.longitude)
}
}
}
}
) {
Text("Get Location")
Text("Get & Save Location")
}
}
}