title: Calculating distance between gps points
body: <p>Trying to calculate from a .gpx file the distance between each gps points. I have tried 2 different formulas.
  This one should be more accurate: <strong>dist = 6378.388 * acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1))</strong></p>
  
  <p>This one should also work for short distances: <strong>distance = sqrt(dx * dx + dy * dy)</strong></p>
  
  <p>The distance between each point is max. 100 m. I calculated the distance with my program and got 26.8 km. Then I have imported the .gpx file into goole earth and I have got a distance of 21 km. I do not know why these values differ so much. Maybe you could help me.</p>
  
  <p>Here comes the code (just the relevant fragments) to calculate the distance from the gpx file:</p>
  
  <pre><code>...
  $xml = file_get_contents($file);
  $doc = new DOMDocument();
  $doc-&gt;loadXML($xml);
  $track = array();
  
  /* get the elevation for each gps point */
  ...
  
  /* get the time for each gps point */
  ...
  
  /* get the position for each gps point*/                                
  $trkpts = $doc-&gt;getElementsByTagName('trkpt');
  $i=0;
  foreach ($trkpts as $trkpt) {
     $track[$i]['position']   = array();
     $track[$i]['position']['lon']    = $trkpt-&gt;getAttribute('lat');
     $track[$i]['position']['lat']    = $trkpt-&gt;getAttribute('lon');
     $i++;
  }
  
  /* calculate the distance for each gps point */
  ini_set('precision', '50');
  for($i2=0;$i2&lt;($i);$i2++){
      if($i2==0){
        $track[$i2]['distance'] = 0;
        /* get first point */
        $lastlat  = deg2rad($track[$i2]['position']['lat']);
        $lastlon  = deg2rad($track[$i2]['position']['lon']);
        $lastele  = $track[$i2]['ele'];
      }
      else{
  
         $aktlat = deg2rad($track[$i2]['position']['lat']);
         $aktlon  = deg2rad($track[$i2]['position']['lon']);
         $aktele  = $track[$i2]['ele'];
  
         /* if coordinates differ calculate distance */                   
         if(($aktlat-$lastlat)!=0 || ($aktlon-$lastlon)!=0){
              $track[$i2]['distance'] = acos(sin($lastlat)*sin($aktlat)+cos($lastlat)*cos($aktlat)*cos($aktlon-$lastlon));
  
              $track[$i2]['distance'] = $track[$i2]['distance'] * 6378137;
  
              /* calculate the elevation difference */
              $track[$i2]['distance'] = sqrt(pow($track[$i2]['distance'],2)+pow(($aktele-$lastele),2));
  
         }
         else{
             $track[$i2]['distance'] = 0;
         }
         /* save act point as last point */
         $lastlat                 = deg2rad($aktlat);
         $lastlon                 = deg2rad($aktlon);
         $lastele             = $aktele;
      }
  }
  </code></pre>
  
  <p>Here is the gpx file for everyone who wants to check: <a href="http://mein-sporttagebuch.de/userfiles/tempfiles/runtastic_20130825_1054_Radfahren.gpx" rel="nofollow">http://mein-sporttagebuch.de/userfiles/tempfiles/runtastic_20130825_1054_Radfahren.gpx</a></p>
  
  <p>Thanks for any advices.</p>
  
  <p><strong>EDIT</strong>
  Here comes the code for getting the elevation (from elevation I calculate some more measures like highest and deepest point):</p>
  
  <pre><code>$eles = $doc-&gt;getElementsByTagName('ele');
  $i=0;
  $minheight  = 0;
  $maxheight  = 0;
  $asc        = 0;
  $desc       = 0;
  $lastheight = 0;
  foreach ($eles as $ele) {
     if($i&gt;0){
        if($lastheight&lt;$ele-&gt;nodeValue){
           $asc +=  $ele-&gt;nodeValue-$lastheight;
        }
        else{
           $desc +=  $lastheight-$ele-&gt;nodeValue;
        }
     }
     $track[$i] = array();
     $track[$i]['ele'] = $ele-&gt;nodeValue;
     if($minheight&gt;$ele-&gt;nodeValue || $i==0){
        $minheight = $ele-&gt;nodeValue;
     }
     if($maxheight&lt;$ele-&gt;nodeValue){
        $maxheight = $ele-&gt;nodeValue;
     }
  
     $lastheight = $ele-&gt;nodeValue;
     $i++;
  }
  </code></pre>