Skip to content

Instantly share code, notes, and snippets.

@RaheemJnr
Created April 11, 2023 15:40
Show Gist options
  • Save RaheemJnr/0215a6265abcdc47b890b6d185e89901 to your computer and use it in GitHub Desktop.
Save RaheemJnr/0215a6265abcdc47b890b6d185e89901 to your computer and use it in GitHub Desktop.
fun getReadableLocation(latitude: Double, longitude: Double, context: Context): String {
var addressText = ""
val geocoder = Geocoder(context, Locale.getDefault())
try {
val addresses = geocoder.getFromLocation(latitude, longitude, 1)
if (addresses?.isNotEmpty() == true) {
val address = addresses[0]
addressText = "${address.getAddressLine(0)}, ${address.locality}"
// Use the addressText in your app
Log.d("geolocation", addressText)
}
} catch (e: IOException) {
Log.d("geolocation", e.message.toString())
}
return addressText
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment