0

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());

        }
}
tom
  • 3
  • 2

1 Answers1

0

try this

  if (ActivityCompat.checkSelfPermission(DriverMap.this, android.Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                    DriverMap.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(DriverMap.this, new String[]{android.Manifest.
                permission.ACCESS_FINE_LOCATION}, 1);
    } else {
        if (!mMap.isMyLocationEnabled())
           mMap.setMyLocationEnabled(true);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        myLocation = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

        if (myLocation == null) {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            String provider = lm.getBestProvider(criteria, true);
            myLocation = lm.getLastKnownLocation(provider);
        }

        if (myLocation != null) {
            latitude = myLocation.getLatitude();
            lonitude = myLocation.getLongitude();
            userLocation = new LatLng(latitude, lonitude);
              mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 12), 1500, null);
             pos = mMap.addMarker(new MarkerOptions().position(userLocation).title("ur loc"));
             pos.setDraggable(true);
              pos.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.car));
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 17));
        }
    }
Ahmad Sabbagh
  • 152
  • 1
  • 11
  • So based on your answer, I take it my concept won't be possible using the fusedLocationProviderClient? Do I still require the LocationCallback method if I switch to a LocationManager? – tom Jun 13 '19 at 20:32
  • you can use both yes , or OnLocationChanged also , – Ahmad Sabbagh Jun 14 '19 at 14:17