61

I stumbled upon this message recently, and I was pretty sure that this constructor wasn't deprecated in prior versions to 18.0.0, but I cannot find information anywhere that this one has been deprecated either.

And what should we use instead, is there another way to create a locationRequest ?

message complaining that LocationRequest() is deprecated

Roar Grønmo
  • 2,926
  • 2
  • 24
  • 37

10 Answers10

134

Yes, the LocationRequest constructor is deprecated. You can use its static method LocationRequest.create() to create a location request.

Kotlin:

locationRequest = LocationRequest.create().apply {
    interval = 100
    fastestInterval = 50
    priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    maxWaitTime = 100
}

Java:

locationRequest = LocationRequest.create()
    .setInterval(100)
    .setFastestInterval(3000) 
    .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
    .setMaxWaitTime(100);

Update

As @Shimon pointed out LocationRequest.PRIORITY_HIGH_ACCURACY is now deprecated, so instead use Priority.PRIORITY_HIGH_ACCURACY

Kunu
  • 5,078
  • 6
  • 33
  • 61
70

LocationRequest.create().apply{ ... } is now also deprecated.

Please use LocationRequest.Builder() instead. I.E. like this:

(locationInterval, locationFastestInterval and locationMaxWaitTime corresponds to the values used before when using create())

        locationRequest = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, locationInterval)
            .setWaitForAccurateLocation(false)
            .setMinUpdateIntervalMillis(locationFastestInterval)
            .setMaxUpdateDelayMillis(locationMaxWaitTime)
            .build()

Please read more here: https://developer.android.com/reference/android/location/LocationRequest.Builder

and here: https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.Builder

Roar Grønmo
  • 2,926
  • 2
  • 24
  • 37
13

This line now deprecated: priority = LocationRequest.PRIORITY_HIGH_ACCURACY

replace with priority = Priority.PRIORITY_HIGH_ACCURACY

5

For anyone who faces this error in geolocator 8.0.1 of Flutter. Try to edit FusedLocationClient.java:199 for now. Then wait for author to update the pub package

From

LocationRequest locationRequest = new LocationRequest();

To

LocationRequest locationRequest = LocationRequest.create();

This is the LocationRequest class enter image description here

ANDYNVT
  • 531
  • 4
  • 19
5

UPDATE:

Constant PRIORITY_HIGH_ACCURACY is deprecated use Priority.PRIORITY_HIGH_ACCURACY instead.

 private fun updateLocationTracking(isTracking: Boolean) {
        if(isTracking) {
            if(TrackingUtility.hasLocationPermissions(this)) {
                val request = LocationRequest.create().apply {
                    interval = LOCATION_UPDATE_INTERVAL
                    fastestInterval = FASTEST_LOCATION_INTERVAL
                    priority =  Priority.PRIORITY_HIGH_ACCURACY
                }
} 
Mado
  • 329
  • 3
  • 16
5

For JAVA, I have fixed it by using below code

private LocationRequest locationRequest;

Inside onCreate()

locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 100)
            .setWaitForAccurateLocation(false)
            .setMinUpdateIntervalMillis(2000)
            .setMaxUpdateDelayMillis(100)
            .build();
3

May 2023 Kotlin:

import com.google.android.gms.location.LocationRequest

locationRequest = LocationRequest.Builder(1000L).setPriority(Priority.PRIORITY_HIGH_ACCURACY).build()
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
  • For Priority like PRIORITY_HIGH_ACCURACY, please use .setPriority(). Also apply is not needed for such requests as they return the same Builder specifically to be chained – George Jun 02 '23 at 07:17
  • @George I updated my answer to show the alternative but what's the difference? – Lance Samaria Jun 02 '23 at 07:59
  • Thank you! As for Builder - not much a difference, just avoiding using two builder patterns at the same time As for .setPriority() - setGranularity() with Priority.* constants gives an error – George Jun 02 '23 at 08:20
1

There are (at least) two different LocationRequests

This post is discussing google maps however for those who are looking for location updates outside google maps there have also been some recent changes

HIGH_ACCURACY is set using setQuality(LocationRequest.QUALITY_HIGH_ACCURACY)

val fastLocationRequests = LocationRequest.Builder(1000L) // interval specified in millis so this is every 1 second
    .setQuality(LocationRequest.QUALITY_HIGH_ACCURACY)
    .setMaxUpdateDelayMillis(0L) // Note 0 represents no batching allowed 
    .build()

Use of the alternative Builder()

val slowLocationRequests = LocationRequest.Builder(fastLocationRequests)
    .setIntervalMillis(60000L) // once per minute
    .build()

Documentation

RatherBeSailing
  • 261
  • 1
  • 11
0
 LocationRequest locationRequest = LocationRequest.create() //if you want access of variable
                            .setInterval(100)
                            .setFastestInterval(3000)
                            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                            .setNumUpdates(1)
                            .setMaxWaitTime(100);
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30737947) – TeachMeJava Jan 07 '22 at 14:10
0

I too got stuck with this. every other day they deprecate one method and post new ones. you can use this code snippet: locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, locationInterval) .setWaitForAccurateLocation(false) .setMinUpdateIntervalMillis(locationFastestInterval) .setMaxUpdateDelayMillis(locationMaxWaitTime) .build(); declare all the variables before.