I am using onLocationChanged
but it isn't giving a very precise location even though I am using the FINE_LOCATION permission. getAccuracy()
usually returns 100.000 (exactly!) even though I can see from the recorded results that the accuracy is sometimes far worse than 100 metres (I am ignoring any results where the accuracy is measured as less than 101),
However, if I run another app (which also uses location) at the same time, my reported accuracy figures are much better (e.g 6.66 metres in the same location as the 100 metres otherwise recorded). So it would appear that my app isn't waiting long enough to get an accurate fix before firing onLocationChanged()
.
I have looked at a possible solution here but this uses GpsStatus.Listener
which was deprecated a while ago, and I cannot find an example of how to use its replacent GnssStatus.Callback
in a similar way.
I could try using the FusedLocationProvider API but unsure if this would fix the problem - anyway I would like to stick to the LocationManager method if possible
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
if (setProvider() == false)
Notify("Location Provider Error");
....
....
public boolean setProvider() {
provider = locationManager.getBestProvider(criteria, true);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return false;
}
locationManager.requestLocationUpdates(provider, UPDATE_TIME, 0, locationListener);
return true;
}
UPDATE_TIME
is normally 2 minutes but I have tried shorter times without success. I am using the location methods in a background service, but the problem still occurs while the app is running in the foreground.
public void onLocationChanged(Location location) {
float accuracy = location.getAccuracy();
if (accuracy < 101){
double lat = location.getLatitude();
double lng = location.getLongitude();
long time = System.currentTimeMillis();
UploadLocation(lat, lng, time);
}