title: calculating distance using GPS point cloud
body: <p>I have an android application in which I am trying to caltulate the distance travelled using coordinates provided by the GPS. I am currently using the method which calculates the lenght of the arc between two points on the globe.</p>
  
  <pre><code>public void onLocationChanged(Location location) 
  {
  Location loc= new Location("");
  loc.setLatitude(prev_latitude);
  loc.setLongitude(prev_longitude);
  distance+=location.distanceTo(loc);
  ...
  }
  </code></pre>
  
  <p>Also, once this is done, the prev_{latitude,longitude} are set to the current latitude/longitude.</p>
  
  <p>The obtained distance of the arc is then added to the accumulated distance (last line of the code).
  Now, this method works fine if the GPS reception is good enough and we have sufficient accuracy. As soon as the accuracy of the acquired coordinates becomes worse, the points start to deviate from the real location and all this error gets integrated into the total distance. 
  Is there a function in android or a separate java library that automatically filters the error and fits the right points/track to a set of raw GPS measurements and thus obtains an accurate estimate of the distance traveled?
  How would one go about this problem if such a library does not exist?
  See the picture below for reference. The method provided in Android and is being used now is shown by the blue line. It calculates distances separately between subsequent coordinates. Because of the error, the distance is not correct. The actual path is shown in red.</p>
  
  <p><img src="https://i.stack.imgur.com/440SR.png" alt="GPS points (black dots),wrong distance (blue), true distance (red)"></p>