title: Google Play services location API - Counting distance
body: <p>I've been following the <a href="https://developer.android.com/training/location/index.html" rel="nofollow">https://developer.android.com/training/location/index.html</a> tutorial.</p>
  
  <p>Here's my class implementing the new Google's Location API:</p>
  
  <pre><code>public class GPSTracker implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
      private final Context context;
      private TextView distanceTextView;
      private GoogleApiClient mGoogleApiClient;
      private LocationRequest mLocationRequest;
      private Location mLastLocation;
      private double totalMeters = 0;
  
      public GPSTracker(Context context, TextView distanceTextView) {
          this.context = context;
          this.distanceTextView = distanceTextView;
          buildGoogleApiClient();
      }
  
      protected synchronized void buildGoogleApiClient() {
          mGoogleApiClient = new GoogleApiClient.Builder(context)
                  .addConnectionCallbacks(this)
                  .addOnConnectionFailedListener(this)
                  .addApi(LocationServices.API)
                  .build();
  
          mGoogleApiClient.connect();
      }
  
      @Override
      public void onLocationChanged(Location location) {
          double distance = mLastLocation.distanceTo(location);
          mLastLocation = location;
          totalMeters += distance;
          distanceTextView.setText(totalMeters + "");
      }
  
      @Override
      public void onConnected(Bundle bundle) {
          mLocationRequest = LocationRequest.create();
          mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
          mLocationRequest.setInterval(1000); // Update location every second
  
          LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
  
          mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
      }
  
      @Override
      public void onConnectionSuspended(int i) {
  
      }
  
      @Override
      public void onConnectionFailed(ConnectionResult connectionResult) {
          Toast.makeText(context, connectionResult.getErrorMessage(), Toast.LENGTH_SHORT).show();
      }
  }
  </code></pre>
  
  <p>Everything seems to be working flawlessly, but when my phone is not moving at all the totalDistance is still rising by 0.2-0.3 meters every time an onLocationChanged method is executed. </p>
  
  <p>Any solution to prevent this situation is highly appreciated.</p>