1

I am using FusedLocationProviderClient to trace location and I want to get the best location by using getAccuracy() method. I checked this method on some devices and it returns 1500 and sometimes 1700 while it has to be less than a few meters(less than 12). Why is this happening and what should I do? Thanks in Advance.

My code:

mLocationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                Location location = locationResult.getLastLocation();
                float acc = location.getAccuracy();
                if (location != null && location.getAccuracy() < 10){
                    if(preLocation == null){
                        preLocation = location;
                        locationChanged(location);
                        sendLocation(location.getLatitude(), location.getLongitude());
                    }else if (preLocation.getLatitude() != location.getLatitude() && preLocation.getLongitude() != location.getLongitude()) {
                        // Logic to handle location object
                        locationChanged(location);
                        preLocation = location;
                    }
                }
            }
        };
Mohsen Hatami
  • 317
  • 3
  • 14
  • GPS might not always be available and the other positioning methods typically aren't that accurate. GPS won't work indoors and getting a satellite fix may take a while especially if the device has no SIM card and assisted GPS isn't available. – Markus Kauppinen Jan 15 '19 at 09:12

1 Answers1

0

Try to set locationRequest priority to High Accuracy like this

LocationRequest mLocationRequest = new LocationRequest();

        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                //Location Permission already granted
                mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
            }
        } else {
            mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
        }
    }
 LocationCallback mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            for (Location location : locationResult.getLocations()) {
                //Do what you want with the location here
            }
        }
    };
Ahmed Karam
  • 386
  • 1
  • 7