반응형
위치 정보 서비스를 얻어 오도록 구현.
구현방법
- AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- LocationTracker.kt
import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
class LocationTracker (val activity: Activity){
companion object{
const val TAG = "LocationTracker"
}
private val MIN_DISTANCE_CHANGE_FOR_UPDATES: Long = 1 //거리
private val MIN_TIME_BW_UPDATES = (3000 * 1 * 1).toLong() //시간
val gpsListener: LocationListener = object : LocationListener {
override fun onLocationChanged(location: Location) {
MyLog.i(TAG, "GPS Location changed, onLocationChanged()")
val latitude = location.latitude
val longitude = location.longitude
MyLog.i(TAG, "GPS Location changed, Latitude: $latitude" + ", Longitude: $longitude")
}
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {
MyLog.i(TAG, "GPS Location changed, onStatusChanged()")
}
override fun onProviderEnabled(provider: String) {
MyLog.i(TAG, "GPS Location changed, onProviderEnabled()")
}
override fun onProviderDisabled(provider: String) {
MyLog.i(TAG, "GPS Location changed, onProviderDisabled()")
}
}
val networkListener: LocationListener = object : LocationListener {
override fun onLocationChanged(location: Location) {
val latitude = location.latitude
val longitude = location.longitude
MyLog.i(TAG, "Network Location changed, Latitude: $latitude" + ", Longitude: $longitude")
}
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {
MyLog.i(TAG, "Network Location changed, onStatusChanged()")
}
override fun onProviderEnabled(provider: String) {
MyLog.i(TAG, "Network Location changed, onProviderEnabled()")
}
override fun onProviderDisabled(provider: String) {
MyLog.i(TAG, "Network Location changed, onProviderDisabled()")
}
}
fun getLocation(): Location? {
val locationManager : LocationManager = activity.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val isGPSEnabled: Boolean = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
val isNetworkEnabled: Boolean = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
val hasFineLocationPermission : Int? = activity?.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
val hasCoarseLocationPermission : Int? = activity?.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
var gpsLocation : Location? = null
var networkLocation : Location? = null
if(!(hasFineLocationPermission == PackageManager.PERMISSION_GRANTED )
&& !(hasCoarseLocationPermission == PackageManager.PERMISSION_GRANTED )){
//없음
MyLog.i(TAG,"위치 퍼미션 권한 없음")
}else{
if(!isGPSEnabled && !isNetworkEnabled){
//없음
MyLog.i(TAG,"GPS/Network Disable")
}
if(isGPSEnabled){
locationManager.removeUpdates(gpsListener)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES.toLong(), MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), gpsListener)
gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
MyLog.i(TAG, "GPS Loaction: ${gpsLocation?.latitude} / ${gpsLocation?.longitude}")
}
if(isNetworkEnabled){
locationManager.removeUpdates(networkListener)
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES.toLong(), MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), networkListener)
networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
MyLog.i(TAG, "Network Loaction: ${networkLocation?.latitude} / ${networkLocation?.longitude}")
}
if(gpsLocation == null && networkLocation ==null){
//return null
}else if(gpsLocation != null){
return gpsLocation
}else if(networkLocation != null){
return networkLocation
}
}
val defaultLocation : Location = Location("영등포구 의사당대로 141")
defaultLocation.latitude = 37.51953
defaultLocation.longitude = 126.92742
return defaultLocation
}
}
참고자료:
[1] https://developer.android.com/training/location/permissions?hl=ko
[2] https://codechacha.com/ko/android-get-location-from-locationmanager/
반응형
'안드로이드' 카테고리의 다른 글
[안드로이드] 앱 알림설정 on off 활성 비활성화 차단 방법 (0) | 2022.04.26 |
---|---|
[안드로이드] 앱(패키지) 설치 유무 확인 (0) | 2022.04.15 |
[Android] EdtitText 필터 적용, 이모지 및 특수문자 입력 차단 (0) | 2021.11.23 |
[Android] data Binding + liveData 적용 주의 ui not updated (1) | 2021.11.11 |
[Android] startActivityForResult deprecated kotlin (0) | 2021.11.10 |