Last active
January 6, 2020 19:22
-
-
Save EbenezerGH/ff987e88eede4d48ab4d11540082cbd8 to your computer and use it in GitHub Desktop.
dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
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 | |
} | |
``` |
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
This part
cos(degToRad(lat1) * cos(degToRad(lat2))) *
contains an error. The parentheses are not closed correctly.