LocationRequest.create() got deprecated. How to fix it?
How to properly ask for location in location services 21.0.0
While I was updating one of my projects, I encountered a deprecation warning in my location handling. One whole section while creating the LocationRequest was crossed-out as deprecated.
Image from Iconfinder by Aleksandr Reva
Location services were updated to version 21.0.0, and the LocationRequest.create was deprecated with the new update. The changes occurred at 13. 10. 2022 in the release notes.
Here is how to change LocationRequest.create to recommended LocationRequest.Builder:
// DEPRECATED
// LocationRequest.create().apply {
// interval = timeInterval
// smallestDisplacement = minimalDistance
// priority = LocationRequest.PRIORITY_HIGH_ACCURACY
// }
// New builder
LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, timeInterval).apply {
setMinUpdateDistanceMeters(minimalDistance)
setGranularity(Granularity.GRANULARITY_PERMISSION_LEVEL)
setWaitForAccurateLocation(true)
}.build()
Fields of LocationRequest used to be filled upon the creation of the LocationRequest. However, new setters are used by the new Builder of the LocationRequest. LocationRequest.PRIORITY_HIGH_ACCURACY and other values are deprecated too.
New constructors
- LocationRequest.Builder(timeInterval) — time in milliseconds, how often you want to get location updates
- LocationRequest.Builder(Priority, timeInterval) — same as above + priority, which now has a separate interface with the same values as before
- LocationRequest.Builder(locationRequest) — other LocationRequest can be used as a starting point for new Builder
The complete example of implementing location request without permission handling is here:
@SuppressLint("MissingPermission")
class LocationManager(
context: Context,
private var timeInterval: Long,
private var minimalDistance: Float
) : LocationCallback() {
private var request: LocationRequest
private var locationClient: FusedLocationProviderClient
init {
// getting the location client
locationClient = LocationServices.getFusedLocationProviderClient(context)
request = createRequest()
}
private fun createRequest(): LocationRequest =
// New builder
LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, timeInterval).apply {
setMinUpdateDistanceMeters(minimalDistance)
setGranularity(Granularity.GRANULARITY_PERMISSION_LEVEL)
setWaitForAccurateLocation(true)
}.build()
fun changeRequest(timeInterval: Long, minimalDistance: Float) {
this.timeInterval = timeInterval
this.minimalDistance = minimalDistance
createRequest()
stopLocationTracking()
startLocationTracking()
}
fun startLocationTracking() =
locationClient.requestLocationUpdates(request, this, Looper.getMainLooper())
fun stopLocationTracking() {
locationClient.flushLocations()
locationClient.removeLocationUpdates(this)
}
override fun onLocationResult(location: LocationResult) {
// TODO: on location change - do something with new location
}
override fun onLocationAvailability(availability: LocationAvailability) {
// TODO: react on the availability change
}
}
For the whole documentation and setters go here: Google Android docs