Search code examples
androidgpslocationfusedlocationproviderapi

Fused location giving accuracy more than 2000 even when device is on High accuracy


I create global class for getting current location for my project. I am getting Location whenever user pressed button on Activity.

public class Get_Current_Location implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
    GoogleApiClient apiClient;
    Location CurrentLocation;
    LocationRequest locationRequest;
    Context context;
    Interface_For_Location getLocation;
    String TAG = "###Current Location###";


    public Get_Current_Location(Context context,Interface_For_Location location) {
        this.context = context;
        this.getLocation=location;
        apiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        Log.d(TAG, "Google API BUILD Successfully " + apiClient);
        apiClient.connect();
        setLocationRequest();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.d(TAG,"On Connected Calling");

        startLocationUpdate();
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d(TAG,"On Location Changed Calling ");
        Log.d(TAG,"Updated Location OnLocationChnaged "+location);
        CurrentLocation=location;
        getLocation.currentLoc(CurrentLocation);
        stopLocationUpdates();
    }

    public void startLocationUpdate() {
        Log.d(TAG,"Start Location Update Calling ");
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, locationRequest, this);
    }

    private void stopLocationUpdates(){
        LocationServices.FusedLocationApi.removeLocationUpdates(apiClient,this);
    }

    private void setLocationRequest(){
        Log.d(TAG,"Set Location Update Request Calling");
        locationRequest=new LocationRequest();
        locationRequest.setInterval(10*60*1000);
        locationRequest.setFastestInterval(10*60*1000);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setNumUpdates(1);
    }    
}

I am getting location via interface on calling class. I have to show distance of two devices under 50 meters but whenever i tried to get location of device which is next to each other, both generates accuracy with more than 2000. With this low accuracy level i can't detect device which is next to each other.

Please guide me to make this accuracy level below 100 so i can detect device under 59 meters.


Solution

  • Sometimes, the device takes time to get a good location (GPS or Network), so you could potentially get very inaccurate points for a short while. You need to add a filter for your desired level of accuracy and only shut the location update down when you get your desired point. Here is an example of how to perform this:

    @Override
    public void onLocationChanged(Location location) {
        Log.d(TAG,"On Location Changed Calling ");
        if (location.getAccuracy() <= MIN_ACCURACY) {
            Log.d(TAG,"Updated Location OnLocationChnaged "+location);
            CurrentLocation=location;
            getLocation.currentLoc(CurrentLocation);
            stopLocationUpdates();
        }
        //Else don't stop the update until a location with good accuracy is received.
    }