Skip to content

Instantly share code, notes, and snippets.

@olayemii
Last active June 8, 2023 23:18
Show Gist options
  • Save olayemii/2a507707d8a70ae1184c6736720c843c to your computer and use it in GitHub Desktop.
Save olayemii/2a507707d8a70ae1184c6736720c843c to your computer and use it in GitHub Desktop.
A dart helper to calculate the distance between two geometric points
import 'dart:math';
double calculateDifference(double lat1, double lon1, double lat2, double lon2) {
double radius = 6371; // km
double changeInLat = toRad(lat2 - lat1);
double changeInLong = toRad(lon2 - lon1);
double lat1Rad = toRad(lat1);
double lat2Rad = toRad(lat2);
double formula = sin(changeInLat / 2) * sin(changeInLat / 2) +
sin(changeInLong / 2) *
sin(changeInLong / 2) *
cos(lat1Rad) *
cos(lat2Rad);
double c = 2 * atan2(sqrt(formula), sqrt(1 - formula));
return radius * c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment