title: Total distance calculation from LatLng List
body: <p>Im using dart/flutter and the <code>'package:latlong/latlong.dart'</code> to parse a GPX file into a list of LatLng objects. That is working fine, but the next step is to find the total distance of the route.</p>
  
  <p>The question here is</p>
  
  <blockquote>
    <p>how can I get the total distance from a list of LatLng objects in
    Dart?</p>
  </blockquote>
  
  <p>I have used Haversine formula before in Java, but not sure the best way to implement it using Dart. Any help of suggestion would be greatly appreciated.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Below is an attempt to get total distance based on the answer below. For some reason though it doesn't add to <code>totalDistance</code> and doesn't return a double.</p>
  
  <p>The below is suppose to iterate through a list of 60+ LatLng objects and find distance between each one, adding to a totalDistance double which is returned at the end of the loop.</p>
  
  <pre><code> static double getDistanceFromGPSPointsInRoute(List&lt;LatLng&gt; gpsList) {
      double totalDistance = 0.0;
  
      for (var i = 0; i &lt; gpsList.length; i++) {
        var p = 0.017453292519943295;
        var c = cos;
        var a = 0.5 -
            c((gpsList[i + 1].latitude - gpsList[i].latitude) * p) / 2 +
            c(gpsList[i].latitude * p) *
                c(gpsList[i + 1].latitude * p) *
                (1 - c((gpsList[i + 1].longitude - gpsList[i].longitude) * p)) /
                2;
        double distance = 12742 * asin(sqrt(a));
        totalDistance += distance;
        print('Distance is ${12742 * asin(sqrt(a))}');
      }
      print('Total distance is $totalDistance');
      return totalDistance;
    }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>flutter: Distance is 0.004143962775784678
  flutter: Distance is 0.0041439635323316775
  flutter: Distance is 0.007796918986828574
  flutter: Distance is 0.007285385943437824
  flutter: Distance is 0.006890844300938902
  flutter: Distance is 0.006353952460010352
  flutter: Distance is 0.005560051252981138
  </code></pre>
  
  <p>As you can see above, there is no mention of Total Distance.</p>
  
  <p><strong>Example of LatLng List</strong></p>
  
  <pre><code>flutter: -36.84975 , 174.646685
  flutter: -36.849692 , 174.646497
  flutter: -36.84967 , 174.646436
  flutter: -36.849578 , 174.646264
  flutter: -36.849502 , 174.646164
  flutter: -36.849367 , 174.646038
  flutter: -36.849209 , 174.645959
  flutter: -36.849155 , 174.64594
  flutter: -36.849107 , 174.645932
  flutter: -36.849058 , 174.645922
  flutter: -36.848952 , 174.645895
  flutter: -36.84886 , 174.645906
  flutter: -36.84879 , 174.645913
  flutter: -36.848748 , 174.645836
  flutter: -36.848744 , 174.645802
  </code></pre>