1function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
2 var R = 6371; // Radius of the earth in km
3 var dLat = deg2rad(lat2-lat1); // deg2rad below
4 var dLon = deg2rad(lon2-lon1);
5 var a =
6 Math.sin(dLat/2) * Math.sin(dLat/2) +
7 Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
8 Math.sin(dLon/2) * Math.sin(dLon/2)
9 ;
10 var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
11 var d = R * c; // Distance in km
12 return d;
13}
14function deg2rad(deg) {
15 return deg * (Math.PI/180)
16}
1 static double coordinateDistance(
2 double lat1, double lng1, double lat2, double lng2) {
3 const p = 0.017453292519943295;
4 const c = cos;
5 final a = 0.5 -
6 c((lat2 - lat1) * p) / 2 +
7 c(lat1 * p) * c(lat2 * p) * (1 - c((lng2 - lng1) * p)) / 2;
8 return 12742 * asin(sqrt(a));
9 }
10
11 // calculate distance from polyline-coordinates (list of locations)
12 static double calculateDistance(
13 {@required List<Location> polylineCoordinates}) {
14 double _totalDistance = 0.0;
15
16 for (int i = 0; i < polylineCoordinates.length - 1; i++) {
17 _totalDistance += coordinateDistance(
18 polylineCoordinates[i].lat,
19 polylineCoordinates[i].lng,
20 polylineCoordinates[i + 1].lat,
21 polylineCoordinates[i + 1].lng,
22 );
23 }
24
25 // distance in KM
26 return _totalDistance;
27 }