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
+1
View File
@@ -54,6 +54,7 @@ dependencies {
implementation(libs.androidx.compose.material3)
implementation(libs.play.services.location)
implementation(libs.accompanist.permissions)
implementation(libs.androidx.datastore.preferences)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
@@ -0,0 +1,47 @@
package com.example.myapplication
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.doublePreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "location_prefs")
class LocationDataStore(private val context: Context) {
private object Keys {
val LATITUDE = doublePreferencesKey("latitude")
val LONGITUDE = doublePreferencesKey("longitude")
val TIMESTAMP = doublePreferencesKey("timestamp")
}
suspend fun saveLocation(latitude: Double, longitude: Double) {
context.dataStore.edit { preferences ->
preferences[Keys.LATITUDE] = latitude
preferences[Keys.LONGITUDE] = longitude
preferences[Keys.TIMESTAMP] = System.currentTimeMillis().toDouble()
}
}
val locationFlow: Flow<LocationData?> = context.dataStore.data.map { preferences ->
val lat = preferences[Keys.LATITUDE]
val lon = preferences[Keys.LONGITUDE]
val timestamp = preferences[Keys.TIMESTAMP]
if (lat != null && lon != null) {
LocationData(lat, lon, timestamp?.toLong())
} else {
null
}
}
}
data class LocationData(
val latitude: Double,
val longitude: Double,
val timestamp: Long? = null
)
@@ -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")
}
}
}
+2
View File
@@ -1,5 +1,6 @@
[versions]
accompanistPermissions = "0.36.0"
datastorePreferences = "1.1.7"
playServicesLocation = "21.3.0"
agp = "8.13.0"
kotlin = "2.0.21"
@@ -13,6 +14,7 @@ composeBom = "2024.09.00"
[libraries]
accompanist-permissions = { module = "com.google.accompanist:accompanist-permissions", version.ref = "accompanistPermissions" }
androidx-datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastorePreferences" }
play-services-location = { module = "com.google.android.gms:play-services-location", version.ref = "playServicesLocation" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }