본문 바로가기

Kotlin

[ j android] 현재위치 -> 위도/경도, 주소 변환

728x90
반응형

위치 권한을 받아와 현재위치로 위도/경도, 주소 변환을 포스팅하겠습니다.

 

 

1. androidmanifest.xml에 permission 추가 및 권한설정

** 해당 포스팅 참고

2022.02.14 - [Kotlin] - [ j android] 권한설정하기 (Permission)

 

[ j android] 권한설정하기 (Permission)

핸드폰에서 카메라나 위치, 저장공간 등등 권한 설정하는 방법을 포스팅하겠습니다. 1. androidmanifest.xml에 권한 설정하고싶은 것을 추가해준다.  ex-1) camera  ex-2) location 2. 체크할 권한을 변수에

yj95.tistory.com

 

2. 현재 위치 위도/경도 변환

해당 소스는 Location 리턴 값으로 받게된다. 

fun getLatLng(locationManager: LocationManager?): Location? {
        var currentLatLng: Location? = null
        var hasFineLocationPermission = ContextCompat.checkSelfPermission( this.applicationContext,
            Manifest.permission.ACCESS_FINE_LOCATION)
        var hasCoarseLocationPermission = ContextCompat.checkSelfPermission( this.applicationContext,
            Manifest.permission.ACCESS_COARSE_LOCATION)

        if(hasFineLocationPermission == PackageManager.PERMISSION_GRANTED &&
            hasCoarseLocationPermission == PackageManager.PERMISSION_GRANTED){
            
            //권한을 받은 경우
           
            val locatioNProvider = LocationManager.NETWORK_PROVIDER
            currentLatLng = locationManager?.getLastKnownLocation(locatioNProvider)

        }else{

        }
        return currentLatLng
    }

소스에서  currentLatLng 변수를 통해 lat, lng 값을 가져 올 수 있다.

- lat : currentLatLng.latitude

- lng : currentLatLng.longitude

 

 

3. 위도/경도를 통해 주소를 가져와보자.

 fun current_location(latitude:Double?,longitude:Double?) : String?{
        var addr:String? = null

		if(userLocation != null){
            var GeoCoder =  Geocoder(this, Locale.KOREAN)
            var ResultList: List<Address>? = null
            try{
                ResultList = GeoCoder.getFromLocation(
                    latitude!!, longitude!!, 1
                )
            }catch(e: IOException){
                e.printStackTrace()
            }
            if(ResultList != null){
                addr = ResultList[0].getAddressLine(0) //주소
            }
        }
        return addr
    }

 

-Address 값을 알고 싶다면 해당 포스팅을 참고하면 된다.

2022.03.04 - [Kotlin] - [j android] Geocoder >> List

 

[j android] Geocoder >> List<Address> 반환 값

Geocoder를 이용해 위도 경도를 주소로 변환할때 반환 값들을 알아보자. Geocoder로 List값을 받게되는데 이때 반환 값을 알 수 있다. getAddressLine(0) full 주소 getAdminArea 시 , 도 getLocality , getSubLoc..

yj95.tistory.com

 

 

 

728x90
반응형