Created
June 24, 2020 03:48
-
-
Save mksantoki/18977ad9d73ffc62ace508b1066b6dcb to your computer and use it in GitHub Desktop.
Android get bearing from two locations
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
fun getBearing(begin: LatLng, end: LatLng): Float { | |
val lat = abs(x = begin.latitude - end.latitude) | |
val lng = abs(x = begin.longitude - end.longitude) | |
if (begin.latitude < end.latitude && begin.longitude < end.longitude) | |
return (Math.toDegrees(Math.atan(lng / lat)).toFloat()) | |
else if (begin.latitude >= end.latitude && begin.longitude < end.longitude) | |
return (((90 - Math.toDegrees(Math.atan(lng / lat))) + 90).toFloat()) | |
else if (begin.latitude >= end.latitude && begin.longitude >= end.longitude) | |
return ((Math.toDegrees(Math.atan(lng / lat)) + 180).toFloat()) | |
else if (begin.latitude < end.latitude && begin.longitude >= end.longitude) | |
return (((90 - Math.toDegrees(Math.atan(lng / lat))) + 270).toFloat()) | |
return -1f | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment