Fused Location Api setSmallestDisplacement ignored/not working
Asked Answered
V

2

10

I am trying to create a service that will receive location updates every x sec and after y distance. I receive updates after x sec but never after y distance. I have tested it multiple times with different values and it seems that setSmallestDisplacement is not working at all. There have been various posts about that matter but without any solution. I would appreciate it if someone could help me or even point me to a different direction.

My Service

public class GPS_Service extends Service implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, LocationListener {

private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private int timethresh;
private int distancethresh;
protected Location mCurrentLocation;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    //Set the desired interval for active location updates, in milliseconds.
    mLocationRequest.setInterval(60* 1000);
    //Explicitly set the fastest interval for location updates, in milliseconds.
    mLocationRequest.setFastestInterval(30* 1000);
    //Set the minimum displacement between location updates in meters
    mLocationRequest.setSmallestDisplacement(1); // float


    return super.onStartCommand(intent, flags, startId);

}

@Override
public void onConnected(Bundle bundle) {
    if (mCurrentLocation == null) {
        mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    }
     //Requests location updates from the FusedLocationApi.      
    startLocationUpdates();

}

@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
    //Toast.makeText(this, ""+mCurrentLocation.getLatitude()+","+mCurrentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
    System.out.println(""+mCurrentLocation.getLatitude()+","+mCurrentLocation.getLongitude());


}

@Override
public void onConnectionSuspended(int i) {
    // The connection to Google Play services was lost for some reason. We call connect() to
    // attempt to re-establish the connection.
    mGoogleApiClient.connect();
}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Toast.makeText(getApplicationContext(),"Location services connection failed with code " + connectionResult.getErrorCode(), Toast.LENGTH_LONG).show();

}

protected void startLocationUpdates() {
    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);

}

}

Vaporing answered 22/3, 2016 at 13:46 Comment(0)
E
6

According to this SO question especially the answer of cgr.

The Displacement that is set for LocationRequest, has no chance of getting the locations if device is still as Displacement takes precedance over Intevals (interval and fastestInterval). As I could guess - Maybe you have passed different LocationRequest object (with no displacement set) to LocationServices.FusedLocationApi.requestLocationUpdates().

The LocationRequest setSmallestDisplacement (float smallestDisplacementMeters)

is set the minimum displacement between location update in meters. By default the value of this is 0. It returns the same object, so that the setters can be chained.

NOTE: Location requests from applications with ACCESS_COARSE_LOCATION and not ACCESS_FINE_LOCATION will be automatically throttled to a slower interval, and the location object will be obfuscated to only show a coarse level of accuracy.

Check this page for more information.

Excitability answered 23/3, 2016 at 9:47 Comment(1)
Deep what should I do in case I need to consider both the parameters together? for example "every 10 seconds or 50 meters, whichever is true first."Alive
A
0

Try something like this:

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();

    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);

    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);

    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    mLocationRequest.setSmallestDisplacement(100); //100 meters
}
Apostle answered 15/10, 2017 at 12:36 Comment(1)
Not sure how this answer got upvotes. You are not solving anything with this especially when we dont even know what your values are which you are setting on LocationRequest.Hunsaker

© 2022 - 2024 — McMap. All rights reserved.