Skip to content

Instantly share code, notes, and snippets.

@mksantoki
Created June 24, 2020 03:48
Show Gist options
  • Save mksantoki/18977ad9d73ffc62ace508b1066b6dcb to your computer and use it in GitHub Desktop.
Save mksantoki/18977ad9d73ffc62ace508b1066b6dcb to your computer and use it in GitHub Desktop.
Android get bearing from two locations
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