Tomáš Repčík - 30. 10. 2022

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

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

Subscribe for more
LinkedIn GitHub Medium