/
AmuMax
/
GeoTracker
Обзор
Документация
Войти
/
AmuMax
/
GeoTracker
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
app/src/main/java/com/example/geotracker/MainActivity.kt
162 строки
6 KB
Amumax
first_commit
24 ноя 2025, 10:33
24 ноя 2025, 10:33
48321bd
Код
Авторство
О чём код?
package com.example.geotracker import android.Manifest import android.content.pm.PackageManager import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.setContent import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.viewinterop.AndroidView import androidx.core.app.ActivityCompat import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleEventObserver import com.example.geotracker.ui.theme.GeoTrackerTheme import com.google.android.gms.location.LocationServices import com.yandex.mapkit.Animation import com.yandex.mapkit.MapKitFactory import com.yandex.mapkit.geometry.Point import com.yandex.mapkit.map.CameraPosition import com.yandex.mapkit.mapview.MapView class MainActivity : ComponentActivity() { private val MAPKIT_API_KEY = "209ab81a-f438-41b5-833c-6de038332997" override fun onCreate(savedInstanceState: Bundle?) { MapKitFactory.setApiKey(MAPKIT_API_KEY) MapKitFactory.initialize(this) super.onCreate(savedInstanceState) val fusedLocationClient = LocationServices.getFusedLocationProviderClient(this) setContent { var location by remember { mutableStateOf<Pair<Double, Double>?>(null) } val context = LocalContext.current val requestPermissionLauncher = rememberLauncherForActivityResult( ActivityResultContracts.RequestPermission() ) { isGranted: Boolean -> if (isGranted) { if (ActivityCompat.checkSelfPermission( context, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) { fusedLocationClient.lastLocation.addOnSuccessListener { loc -> if (loc != null) { location = Pair(loc.latitude, loc.longitude) } } } } } LaunchedEffect(Unit) { if (ActivityCompat.checkSelfPermission( context, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) { requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION) } else { fusedLocationClient.lastLocation.addOnSuccessListener { loc -> if (loc != null) { location = Pair(loc.latitude, loc.longitude) } } } } GeoTrackerTheme { Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { Column( modifier = Modifier.fillMaxSize() ) { Text( text = location?.let { "Текущие координаты:\n${it.first}, ${it.second}" } ?: "Ожидание координат...", modifier = Modifier .fillMaxWidth() .padding(8.dp), textAlign = TextAlign.Center ) Box( modifier = Modifier .fillMaxWidth() .weight(1f) ) { MapScreen(latitude = location?.first, longitude = location?.second) } } } } } } override fun onStart() { super.onStart() MapKitFactory.getInstance().onStart() } override fun onStop() { MapKitFactory.getInstance().onStop() super.onStop() } } @Composable fun MapScreen(latitude: Double?, longitude: Double?) { val context = LocalContext.current val mapView = remember { MapView(context) } val lifecycle = LocalLifecycleOwner.current.lifecycle DisposableEffect(lifecycle, mapView) { val observer = LifecycleEventObserver { _, event -> when (event) { Lifecycle.Event.ON_START -> mapView.onStart() Lifecycle.Event.ON_STOP -> mapView.onStop() else -> {} } } lifecycle.addObserver(observer) onDispose { lifecycle.removeObserver(observer) } } AndroidView({ mapView }) { mapView -> if (latitude != null && longitude != null) { mapView.map.move( CameraPosition(Point(latitude, longitude), 16.0f, 0.0f, 0.0f), Animation(Animation.Type.SMOOTH, 1f), null ) mapView.map.mapObjects.clear() mapView.map.mapObjects.addPlacemark(Point(latitude, longitude)) } } }