-
-
Save MehdiFal/08004ad81103bbe6fa0ba0aaef6e0943 to your computer and use it in GitHub Desktop.
This file contains 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
// 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