4

I am facing a issue with getting latitude longitude using FusedLocationProviderClient only in Android Pie, I got the correct location below Android Version 9.0(Pie)

Every time I called getLocation() FusedLocationProviderClient provides same lat long.

  mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        if (location != null) {
                            wayLatitude = location.getLatitude();
                            wayLongitude = location.getLongitude();
                            Log.e("Location11", "" + String.format(Locale.US, "%s - %s", wayLatitude, wayLongitude));

                        } else {
                            mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
                        }
                    }
                });

I got same lat long everytime. like latitude- 28.303303303303 longtitude- 77.411411411

Prateek Gupta
  • 148
  • 11
  • The "last known location" doesn't update unless some application calls "requestLocationUpdates()". Maybe you are stuck with a not up-to-date location on that device(?) Opening Google Maps and waiting for it to point to your current location would help in that case. A real solution would be to always request location updates in your own app. You can then cancel the request after the first result if you don't need continuous updates. – Markus Kauppinen Jun 05 '19 at 10:33
  • 1
    @MarkusKauppinen I did the same but It perfectly works below Android Pie – Prateek Gupta Jun 05 '19 at 10:46

2 Answers2

2

Use FusedLocationProviderApi and set LocationRequest priority to PRIORITY_HIGH_ACCURACY

This is newest API for accurate location fetch and google suggest to use the same.

Check Accuracy details (Android Location Providers - GPS or Network Provider?)

Basically Google play services API has intelligence to get accurate location by fusing GPS+NetworkProvider+passive providers.

Rohit Singh
  • 403
  • 4
  • 9
  • But FusedLocationProviderApi is deprecated, FusedLocationProviderClient is the newest one as I read [FusedLocationProviderClient](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient) document – Prateek Gupta Jun 05 '19 at 10:45
1

Provide Permission Both:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

ACCESS_FINE_LOCATION will give proper result.

RKRK
  • 1,284
  • 5
  • 14
  • 18
Sagar Sah
  • 55
  • 1
  • 8