Skip to content

Instantly share code, notes, and snippets.

@MehdiFal
Created November 24, 2017 13:44
Show Gist options
  • Save MehdiFal/08004ad81103bbe6fa0ba0aaef6e0943 to your computer and use it in GitHub Desktop.
Save MehdiFal/08004ad81103bbe6fa0ba0aaef6e0943 to your computer and use it in GitHub Desktop.
// in meter
private double calculateDistance (Location newLocation, Location oldLocation) {
if (newLocation.hasSpeed ()) {
return newLocation.getSpeed ();
}
if (oldLocation == null) {
return 0;
}
double oldLat = oldLocation.getLatitude ();
double oldLng = oldLocation.getLongitude ();
double newLat = newLocation.getLatitude ();
double newLng = newLocation.getLongitude ();
double dLat = Math.toRadians (newLat - oldLat);
double dLon = Math.toRadians (newLng - oldLng);
double a = Math.sin (dLat / 2) * Math.sin (dLat / 2) +
Math.cos (Math.toRadians (oldLat)) * Math.cos (Math.toRadians (newLat)) * Math.sin (dLon / 2) * Math.sin (dLon / 2);
double c = 2 * Math.asin (Math.sqrt (a));
return Math.round (6371000 * c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment