title: Advice on computing walked distance in web-app
body: <p>I'm currently building an web-app (in Polymer using Mapbox) where users can see featured routes on a map and walk them. They asked me if it would be possible to add a button (start/stop) to record their activity and calculate the number of kilometers walked. The way I would build this is to store each new geopoint on a location change event (<code>navigator.geolocation.watchPosition</code>) and calculate the distance between all points and sum those. </p>
  
  <p><strong>Now the real problem</strong> </p>
  
  <p>There will probably be users that forget to click the stop button again. If this happens the application will keep on sending data to my server. This will result in unnecessary data being pushed to the database. </p>
  
  <p>Now I tried to find a solution for this:</p>
  
  <p><strong>Option #1</strong></p>
  
  <p>I came across the option to check the active tab in the browser making use of the <a href="https://www.w3.org/TR/page-visibility/" rel="nofollow noreferrer">Page Visibility API</a>. In this way I could automatically stop/start tracking. </p>
  
  <pre><code>if (!document.hidden) {
      // resume tracking
  } else {
      // stop tracking
  }
  </code></pre>
  
  <p>However, usually the users of my app will lock their screens which will also stop tracking because <code>visibilityState</code> will be <code>hidden</code>.</p>
  
  <p><strong>Option #2</strong>
  My client came with the solution to automatically stop the timer after x hours. However, this doesn't sound intuitive because this will still result into a strange sum of kilometers. </p>
  
  <p><strong>Option #3</strong>
  Automatically stop tracking walked distance if you're more than x meters away from a featured route. This however will require quite some computation.</p>
  
  <p><strong>Option #4</strong>
  Track all user activity. E.g. pinching, clicking, zooming etc on the map and stop tracking after 30 minutes of no activity</p>
  
  <p>How should I design this app to make sure that the tracking makes sense?</p>