A Simple Android Library to get current location.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.abdullahalshafi:BasicLocationPicker:1.0.4'
}
LocationHelper.start(this, launcher, true) //send true if you want high accuracy
private val launcher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val location = result.data?.getParcelableExtra<Location>(LocationHelper.LOCATION_RESULT) as Location
Toast.makeText(this, "location: ${location.latitude} ${location.longitude}", Toast.LENGTH_LONG).show()
Toast.makeText(this, "address: ${LocationHelper.getAddressFromLocation(this, location)}", Toast.LENGTH_LONG).show()
}
}
val address = LocationHelper.getAddressFromLocation(context, location)