I'm currently developing a location tracking app requiring high accuracy location updates. This is my very first app and I might be in over my head but I am learning as I go. The accuracy that I'm getting from the fusedLocationProviderClient when using location.distanceTo() is shiftingbetween 15 and 70+ meters per update (5sec interval) while walking at a constant pace. Is there a way to improve the accuracy or set a minimum accuracy?
I found a getAccuracy() and hasAccuracy() method in the documentation but so far failed to understand how I could use these methods to get rid of the fluctuating accuracy.
EDIT: I came across the option of adding Criteria to my location code. Could you provide me with an example of how to incorporate that into my code to get consistently high accuracy?
Alternatively, would it be better to change my code to use LocationManager and GPS satellites exclusively since all my sessions are done outside?
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mGoogleMap.setMinZoomPreference(8.0f);
mGoogleMap.setMaxZoomPreference(25.0f);
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(3000);
mLocationRequest.setFastestInterval(1000);
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());
mGoogleMap.setMyLocationEnabled(true);
} else {
//Request Location Permission
checkLocationPermission();
}
}
else {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
mGoogleMap.setMyLocationEnabled(true);
}
}
LocationCallback mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
float zoomLevel = mGoogleMap.getCameraPosition().zoom;
List<Location> locationList = locationResult.getLocations();
System.out.println(workoutLocationList);
if (locationList.size() > 0) {
//The last location in the list is the newest
Location location = locationList.get(locationList.size() - 1);
Location lastLocation = location;
locationList.add(location);
workoutLocationList.add(location);
if(workoutLocationList.size() > 1) {
lastLocation = workoutLocationList.get(workoutLocationList.size() - 2);
}
System.out.println(workoutLocationList.size());
for (Location i : workoutLocationList) {
System.out.println(i);
}
System.out.println("current location is: "+ location);
System.out.println("previous location is: "+ lastLocation);
distanceBetweenLocations += location.distanceTo(lastLocation);
workoutTotalDistance+= distanceBetweenLocations;
System.out.println((int)distanceBetweenLocations);
Log.i("MapsActivity", "Location: " + location.getLatitude() + " " + location.getLongitude());
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
}
}