How to return address from GeocodeListener in Android Geocoder library?

127 Views Asked by At

I want to create a function that takes in a latitude and longitude value (and the current context), then return a concatanated string such that it shows the city and country of the lat, long values. Like this - "City, Country". I am using the android.location.Geocoder library.

Here is the function

fun getAddress(lat : Double, long : Double, context: Context): String {
   val geocoder = Geocoder(context, Locale.getDefault())
   val geocodeListener = Geocoder.GeocodeListener { addresses ->
       val city = addresses[0].adminArea
       val country = addresses[0].countryName
   }
   if(Build.VERSION.SDK_INT >= 33){
       val list = geocoder.getFromLocation(lat, long, 1, geocodeListener)
       //How to return city + country here?

   } else {
       val list = geocoder.getFromLocation(lat, long, 1) /*Deprecated version*/
       //How to return city + country here?
   }
}
0

There are 0 best solutions below