title: Storing coordinates (geolocation) in array to calculate distance
body: <p>Me and  my partner are working on an exercise web application for our school thesis. We are trying to get a runner's position and track him during his workout with the watchposition() function of the geolocation api. On-screen the runner can check his current distance. </p>
  
  <p>This is the point were we are stuck, we can only calculate the distance between the start and end point but when the course is a circle the total distance covered displays zero. </p>
  
  <p>So we had the solution to store multiple coordinates in an array and calculate the sum of those distances but we aren't the most skilled persons in javascript. I hope this makes a little sense if you want more info just ask. </p>
  
  <p>UPDATE: this is what i got so far</p>
  
  <pre><code>    &lt;script&gt;
  
          function startTracking() {
  
              var startPos;   
              var coords = [];
  
              // Reused code - copyright Moveable Type Scripts - retrieved May 4,2010.
              // http://www.movable-type.co.uk/scripts/latlong.html
              // Under Creative Commons License http://creativecommons.org/licenses/by/3.0/
  
              function calculateDistance(lat1, lon1, lat2, lon2) {
                  var R = 6371000; // m
                  var dLat = (lat2-lat1).toRad();
                  var dLon = (lon2-lon1).toRad();
                  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                          Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
                          Math.sin(dLon/2) * Math.sin(dLon/2);
                  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
                  var d = R * c;
                  return d.toFixed(2);    }
  
              Number.prototype.toRad = function() {
                  return this * Math.PI / 180;    }
  
              if (navigator.geolocation) {
                  watchID = navigator.geolocation.watchPosition(function(position) {
                      currentLat = position.coords.latitude;
                      currentLon = position.coords.longitude;
  
                      coords.push([currentLat, currentLon]);
  
                      console.log(coords);
  
                  }, function(error) {
                      alert("Error occurred. Error code: " + error.code);
                      // error.code can be:
                      //   0: unknown error
                      //   1: permission denied
                      //   2: position unavailable (error response from locaton provider)
                      //   3: timed out
                  });
                  // end error    };      // end if  }; // end startTracking
  
          function stopTracking() {
  
              if (navigator.geolocation) {
                 navigator.geolocation.clearWatch(watchID);   }
  
              };  
  &lt;/script&gt;
  </code></pre>
  
  <p>So now my question is how do i loop over my array to calculate the distance between all the given coordinates to get a (more or less) accurate distance?</p>