// 예시, Androidmanifest.xml
<uses-permission android:name ="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
https://developer.android.com/training/location/permissions?hl=ko
1) 위치 서비스 클라이언트 만들기
private lateinit var fusedLocationClient: FusedLocationProviderClient
override fun onCreate(savedInstanceState: Bundle?) {
// ...
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
}
2) 마지막으로 알려진 위치 가져오기
if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
fusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
//Got last know location. In Some rare situation this can be null
}
}
// 권한 문제해결을 위해 if문이 들어가야함.
3) 위치설정변경
fun createLocationRequset(){
val locationRequest = LocationRequest.create()?.apply{
interval = 5000
fastestInterval = 3000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
// 현재 위치 설정받기
if(locationRequest != null){
val builder = LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest)
val client: SettingsClient = LocationServices.getSettingsClient(this)
val task:Task<LocationSettingsResponse> = client.checkLocationSettings(builder.build())
// tak가 완료되면 LocationSettingsResponse객체의 상태 코드로 위치설정을 확인
task.addOnSuccessListener { locationSettingsResponse ->
// All location settings are satisfied. The Client can initialize
// locaiton resquest here
// ...
}
// 사용자에게 위치설정을 변경하라는 메시지 표시
task.addOnFailureListener { exception ->
if(exception is ResolvableApiException){
// Location settings are not setistfy
// by showin the user a dialog
try{
//Show the dialog by calling startResolutionForResult()
// and check the result in onActivityResult().
exception.startResolutionForResult(this@MainActivity, Log.e("error","error"))
} catch (sendEx: IntentSender.SendIntentException){
// Ingnore Error
}
}
}
}
}
import android.content.ContentValues.TAG
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Looper
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.app.ActivityCompat
import com.example.scatter.ui.theme.ScatterTheme
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.LocationSettingsRequest
class MainActivity : ComponentActivity() {
private lateinit var fusedLocationClient: FusedLocationProviderClient
private lateinit var locationCallback: LocationCallback
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ScatterTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
}
}
}
}
// 통합 위치 제공자 초기화
private fun initLocationClient(){
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
val locationRequest = LocationRequest.create()?.apply{
interval = 1000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
val builder = LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest!!)
val client = LocationServices.getSettingsClient(context)
val task = client.checkLocationSettings(builder.build())
task.addOnSuccessListener {
Log.d(TAG, "location client setting success")
}
task.addOnFailureListener {
Log.d(TAG, "location client setting fail")
}
}
// 마지막으로 알려진 위치 요청
fun requestLastLocation(){
if(ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
)!= PackageManager.PERMISSION_GRANTED&&ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return
}
fusedLocationClient.lastLocation
.addOnSuccessListener { location ->
listener.onLocationUpdated(location)
}
}
//위치 업데이트 초기화
private fun iniLocationCallback(){
locationCallback = object : LocationCallback(){
override fun onLocationResult(locationResult: LocationResult?){
locationResult ?: return
for(location in locationResult.locations){
listener.onLocationUpdated(location)
break
}
}
}
}
fun startLocationUpdates(){
if(ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return
}
val locationRequest = LocationRequest.create()?.apply{
interval = 1000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
fusedLocationClient.requestLocationUpdates(
locationRequest!!,
locationCallback,
Looper.getMainLooper()
)
}
}