4

LocationRequest is now deprecated? What is the replacement for it?

I was using it and recently got a deprecation message. What should I do now:

val locationRequest = LocationRequest().apply {
            interval = LOCATION_UPDATE_INTERVAL
            fastestInterval = LOCATION_FASTEST_INTERVAL
            priority = LocationRequest.PRIORITY_HIGH_ACCURACY
        }
        fusedLocationProviderClient.requestLocationUpdates(
            locationRequest,
            locationCallback,
            Looper.getMainLooper()
        )
Stefan
  • 2,829
  • 5
  • 20
  • 44

3 Answers3

5

You can use it like this

val locationRequest = LocationRequest.create().apply {
        interval = LOCATION_UPDATE_INTERVAL
        fastestInterval = LOCATION_FASTEST_INTERVAL
        priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    }
CoolMind
  • 26,736
  • 15
  • 188
  • 224
Divij Gupta
  • 165
  • 9
2

Location services were updated to version 21.0.0, and the LocationRequest.create was deprecated with the new update.

Here is how to change LocationRequest.create to recommended LocationRequest.Builder:

   fun createLocationRequest() = LocationRequest.Builder(
        Priority.PRIORITY_HIGH_ACCURACY, TimeUnit.MINUTES.toMillis(5)
    ).apply {
        setGranularity(Granularity.GRANULARITY_PERMISSION_LEVEL)
        setDurationMillis(TimeUnit.MINUTES.toMillis(5))
        setWaitForAccurateLocation(true)
        setMaxUpdates(1)
    }.build()
Shahab Saalami
  • 862
  • 10
  • 18
1

// The desired interval for location updates. Inexact. Updates may be more or less frequent.

  private const val UPDATE_INTERVAL_IN_MILLISECONDS: Long = 5000
  val  locationRequest = LocationRequest.Builder(
        Priority.PRIORITY_HIGH_ACCURACY, UPDATE_INTERVAL_IN_MILLISECONDS
    ).build()