if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
LOCATION_PERMISSION_REQUEST_CODE
)
fusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
// Handle location
}
.addOnFailureListener { e ->
// Handle error
}
if (location != null) {
val latitude = location.latitude
val longitude = location.longitude
binding.tvLocation.text = "Latitude: $latitude\nLongitude: $longitude"
} else {
binding.tvLocation.text = "Location not available"
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
// Handle permission result
}