0

I have used Fused Location API to get location updates. I am getting updates while setting LocationRequest based on setTimeInterval. But I need only updates on 10m Movement. So i placed setSmallestDisplacement(10) .But when i put setSmallestDisplacement(10), i didnt get updates. Following is my LocationUpdate class

public class LocationManger implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {

GoogleApiClient mLocationClient;
Location mCurrentLocation;
LocationRequest mLocationRequest;
Context mContext;


public LocationManger(Context context) {
    super();
    mLocationClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mContext = context;
}


@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;

}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Helper.showToast("failed", mContext);

}

@Override
public void onConnected(Bundle arg0) {

if (mLocationRequest == null) {
        mLocationRequest = LocationRequest.create();
         mLocationRequest.setSmallestDisplacement(10);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    }

    LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this);

}

@Override
public void onConnectionSuspended(int i) {
    mLocationClient.connect();

}

/**
 * Function to start the location updates
 */
public void startLocationUpdates() {
    if (!mLocationClient.isConnected())
        mLocationClient.connect();
}

/**
 * function to stop the location updates
 */
public void stopUpdatingLocation() {
    if (mLocationClient.isConnected()) {

     LocationServices.FusedLocationApi.removeLocationUpdates(mLocationClient, this);
    }
    mLocationClient.disconnect();

}

/**
 * function to get the current latitude
 */
public double getCurrentLatitude() {
    if (mCurrentLocation != null) {
        return mCurrentLocation.getLatitude();
    }
    return 0;

}

/**
 * function to get the current longitude
 */
public double getCurrentLongitude() {
    if (mCurrentLocation != null) {
        return mCurrentLocation.getLongitude();
    }
    return 0;
}

/**
 * function to get the current location
 */
public Location getCurrentLocation() {
    return LocationServices.FusedLocationApi.getLastLocation(mLocationClient);
}

}

Please give me a solution if anyone know the issue. Thanks in advance

user3506595
  • 129
  • 11

1 Answers1

0

Use setFastestInterval(0) to allow updates more frequently than called for by setInterval. Otherwise you will miss some updates from displacement

John Fontaine
  • 1,019
  • 9
  • 14