Search code examples
androidlocationmanagerlocation-client

Why Location client gives very far location from my current location.?


I am developing a location based app which have the functionality to update user current location in every 1 minutes.

I am using bellow to code for requesting location updates:

private LocationRequest mLocationRequest;
private static LocationClient mLocationClient;

mLocationRequest = LocationRequest.create();

mLocationRequest.setInterval(60000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setFastestInterval(60000);

mLocationClient = new LocationClient(this, this, this);

if (servicesConnected()) {
    mLocationClient.connect();
}

servicesConnected() is user defined method which returns true if Google play services is available otherwise returns false and my overriden method like this:

@Override
public void onConnected(Bundle connectionHint) {
try {
        mLocationClient.requestLocationUpdates(mLocationRequest, this);
    } catch (IllegalStateException e) {
        // TODO: handle exception
    }
 }

@Override
public void onLocationChanged(Location location) {
// logic to store location data

}

But I found location updates like bellow figure while my GPS is ON :

enter image description here

Please suggest what should I do to overcome unwanted location updates.


Solution

  • Here's some info from OwnTracks https://github.com/owntracks/android/issues/66

    From my research the only thing you can do is filter out "bad" locations like this:

    @Override
            public void onLocationChanged(Location location) {
                if(location.getAccuracy() < 210.0) {
                    // use provided location
                }
            }
    

    It doesn't appear that there is any way to prevent these updates from being requested using the Fused provider (other than stopping updates or increasing the interval when you have a location you are satisfied with). There's only so much "filtering" that can be done by the GPS itself and those options are the constants inside LocationRequest that you already know about. I believe your issue is hardware related or has to do with the location you are getting updates from (I'm basing this assumption on the OwnTracks data). Google could theoretically offer more advanced Criteria like it used to do with LocationManager, but I believe that would basically do the same thing as my example, except under the hood (i.e. the GPS would still do the work of getting the location and then discard it AFTER it knows the accuracy isn't high enough).

    If you want to make it do less work, your best options are increasing the interval or simply stopping updates when you no longer need new locations. For example, if you have a decent location, maybe you raise the interval and keep that location longer. But that depends on what your app is trying to do with the data.