Open philipy355 opened 8 months ago
googleMapKey 실제값 확인 : https://console.cloud.google.com/google/maps-apis/
프로젝트 코드 관련
manifest 권한
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Composable MyMap, rememberMapView
@Composable fun MyMap( viewModel: MainViewModel ){ val map = rememberMapView() val state = viewModel.state.value AndroidView( factory = { map }, update = {mapView -> mapView.getMapAsync {googleMap -> state.location?.let{ val latLng = LatLng(it.latitude, it.longitude) googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17f)) googleMap.addPolyline(state.polylineOptions) } } } ) } @SuppressLint("RememberReturnType") @Composable fun rememberMapView() : MapView{ val context = LocalContext.current val mapView = remember { MapView(context) } val lifecycleOwner = LocalLifecycleOwner.current DisposableEffect(lifecycleOwner){ val observer = LifecycleEventObserver { _, event -> when(event){ Lifecycle.Event.ON_CREATE -> mapView.onCreate(Bundle()) Lifecycle.Event.ON_START -> mapView.onStart() Lifecycle.Event.ON_RESUME -> mapView.onResume() Lifecycle.Event.ON_PAUSE -> mapView.onPause() Lifecycle.Event.ON_STOP -> mapView.onStop() Lifecycle.Event.ON_DESTROY -> mapView.onDestroy() else -> throw IllegalStateException() } } lifecycleOwner.lifecycle.addObserver(observer) onDispose { lifecycleOwner.lifecycle.removeObserver(observer) } } return mapView }
MainActivity
class MainActivity : ComponentActivity() { @OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class) @SuppressLint("UnusedMaterial3ScaffoldPaddingParameter", "UnusedMaterialScaffoldPaddingParameter" ) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { var granted by remember { mutableStateOf(false) } val launcher = rememberLauncherForActivityResult( contract = ActivityResultContracts.RequestPermission(), onResult = {isGranted -> granted = isGranted } ) if ((ContextCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION, ) == PackageManager.PERMISSION_GRANTED) ){ granted = true } if(granted){ val viewModel = viewModel<MainViewModel>() lifecycle.addObserver(viewModel) MyMap(viewModel = viewModel) }else { Column( modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { Text("권한이 허용되지 않았습니다.") Button(onClick = { launcher.launch(Manifest.permission.ACCESS_FINE_LOCATION) }){ Text("권한 요청") } } } } } }
MainViewModel
class MainViewModel(application: Application) : AndroidViewModel(application), LifecycleObserver{ private val fusedLocationProviderClient = FusedLocationProviderClient(application.applicationContext) private val locationRequest : LocationRequest private val locationCallback : MyLocationCallBack private val _state = mutableStateOf(MapState(null, PolylineOptions().width(5f).color(android.graphics.Color.RED))) val state : State<MapState> = _state init { locationCallback = MyLocationCallBack() locationRequest = LocationRequest.create() locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY locationRequest.interval = 10000 locationRequest.fastestInterval = 5000 } @SuppressLint("MissingPermission") @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) private fun addLocationListener(){ fusedLocationProviderClient.requestLocationUpdates( locationRequest, locationCallback, Looper.getMainLooper() ) } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) private fun removeLocationListener(){ fusedLocationProviderClient.removeLocationUpdates(locationCallback) } inner class MyLocationCallBack : LocationCallback(){ override fun onLocationResult(locationResult: LocationResult) { super.onLocationResult(locationResult) val location = locationResult.lastLocation val polylineOptions = state.value.polylineOptions _state.value = state.value.copy( location = location, polylineOptions = polylineOptions.add(LatLng(location.latitude, location.longitude)) ) } } } data class MapState( val location : Location?, val polylineOptions: PolylineOptions, )
googleMapKey 실제값 확인 : https://console.cloud.google.com/google/maps-apis/
프로젝트 코드 관련
manifest 권한
Composable MyMap, rememberMapView
MainActivity
MainViewModel