38

I'm trying to set up location updates in my Android app using com.google.android.gms:play-services-location:12.0.0, but I'm getting the following error:

LocationRequest constructor is marked as internal and should not be accessed from apps

My location updates request looks like this:

locationClient.requestLocationUpdates(
    new LocationRequest()
        .setInterval(5000)
        .setFastestInterval(1000)
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY),
    locationCallback,
    null
);

I have followed the docs and the example, which do it the same way. If I'm not supposed to call new LocationRequest(), then what is the proper way to do it?

Duncan Lukkenaer
  • 12,050
  • 13
  • 64
  • 97

2 Answers2

73

Use static methodLocationRequest create ().

 LocationRequest locationRequest = LocationRequest.create();
 locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
 locationRequest.setInterval(5000);
 locationRequest.setFastestInterval(1000);
ADM
  • 20,406
  • 11
  • 52
  • 83
  • 1
    It is mentioned in doc i guess but not clearly . if you see the [Source code](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/location/java/android/location/LocationRequest.java) then you will see the constructor is hide . And also constructor is no where mentioned in docs so `create()` is the ultimate solution. – ADM Mar 26 '18 at 10:31
  • 1
    @ADM Where is it mentioned? The doc says: "Create the location request and set the parameters as shown in this code sample: protected void createLocationRequest() { LocationRequest mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(10000); mLocationRequest.setFastestInterval(5000); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); } If you do this you get the annoying warning. The documentation is real crap most of the time. – The incredible Jan Mar 29 '18 at 06:11
  • 2
    Yeah you are right about the Doc. But what in look up is [LocationRequest](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest) class . And there is no public constructor listed . And also if you go through the source code you will see than constructor is hide . I can not say much about the Doc but to me it seems a bit misleading. – ADM Mar 29 '18 at 06:22
15

LocationRequest initialization procedure has changed into latest Google Play Service dependencies ( > 12.0.0). Now you can use its create() method to initialize this. e.g.

LocationRequest request = LocationRequest.create();
Rahul Sharma
  • 12,515
  • 6
  • 21
  • 30