Skip to content

Instantly share code, notes, and snippets.

@EbenezerGH
Last active January 6, 2020 19:22
Show Gist options
  • Save EbenezerGH/ff987e88eede4d48ab4d11540082cbd8 to your computer and use it in GitHub Desktop.
Save EbenezerGH/ff987e88eede4d48ab4d11540082cbd8 to your computer and use it in GitHub Desktop.
dart
```
import 'dart:math';
var R = 6371e3; // earth radius
num degToRad(num deg) => deg * (pi / 180.0);
num radToDeg(num rad) => rad * (180.0 / pi);
/*
Haversine formula
a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
c = 2 ⋅ atan2( √a, √(1−a) )
d = R ⋅ c
*/
double calculateDistance(double lat1, double lng1, double lat2, double lng2) {
var deltaLat = degToRad(lat2 - lat1);
var deltaLng = degToRad(lng2 - lng1);
var a = sin(deltaLat / 2) * sin(deltaLat / 2) +
cos(degToRad(lat1)) * cos(degToRad(lat2)) *
sin(deltaLng / 2) *
sin(deltaLng / 2);
var c = 2 * atan2(sqrt(a), sqrt(1 - a));
var d = R * c;
return d; // returns m
}
```
@vovahost
Copy link

vovahost commented Jan 6, 2020

This part cos(degToRad(lat1) * cos(degToRad(lat2))) * contains an error. The parentheses are not closed correctly.

@EbenezerGH
Copy link
Author

This part cos(degToRad(lat1) * cos(degToRad(lat2))) * contains an error. The parentheses are not closed correctly.

Good catch man, fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment