Please provide me the android kotlin example code for getting the current location using FusedLocationProviderClient?
CodePudding user response:
Use following code..
private lateinit var fusedLocationClient: FusedLocationProviderClient
override fun onCreate(savedInstanceState: Bundle?) {
// ...
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationClient.lastLocation
.addOnSuccessListener { location : Location? ->
val latitude = location.latitude
val longitude = location.longitude
}
}
Note
You must declare location permissions in your Manifest.xml
file as well
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Dependency:
implementation "com.google.android.gms:play-services-location:21.0.1"