Original Answer
This is happening because FusedLocationProviderApi
deprecated in a recent version of google play services. You can check it here. The official guide now suggests using FusedLocationProviderClient. You can find the detailed guide here.
for e.g inside onCreate()
or onViewCreated()
create a FusedLocationProviderClient
instance
Kotlin
val fusedLocationClient = LocationServices.getFusedLocationProviderClient(requireContext())
and for requesting the last known location all you have to do is call
fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? ->
location?.let { it: Location ->
// Logic to handle location object
} ?: kotlin.run {
// Handle Null case or Request periodic location update https://developer.android.com/training/location/receive-location-updates
}
}
Java
FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(requireContext());
and
fusedLocationClient.getLastLocation().addOnSuccessListener(requireActivity(), location -> {
if (location != null) {
// Logic to handle location object
} else {
// Handle null case or Request periodic location update https://developer.android.com/training/location/receive-location-updates
}
});
Simple, Isn’t it?
Important Update (October 24, 2017):
Yesterday Google updated its official developer page with a warning that says
Please continue using the FusedLocationProviderApi class and don’t migrate to the FusedLocationProviderClient class until Google Play services version 12.0.0 is available, which is expected to ship in early 2018. Using the FusedLocationProviderClient before version 12.0.0 causes the client app to crash when Google Play services is updated on the device. We apologize for any inconvenience this may have caused.
So I think we should continue using the deprecated LocationServices.FusedLocationApi
until Google resolves the issue.
Latest Update (November 21, 2017):
The warning is gone now. Google Play services 11.6 November 6, 2017, release note says : I think Play Services won’t crash when it updates itself in the background. So we can use new FusedLocationProviderClient
now.