Skip to content

Instantly share code, notes, and snippets.

@vs-krasnov
Created November 25, 2018 17:14
Show Gist options
  • Save vs-krasnov/a06421cdeb1668f3f3510cbfc2149c23 to your computer and use it in GitHub Desktop.
Save vs-krasnov/a06421cdeb1668f3f3510cbfc2149c23 to your computer and use it in GitHub Desktop.
Simple function to calculate a new point's latitude and longitude based on provided start point, bearing and distance.
fun getPointByDistanceAndBearing(lat: Double, lon: Double, bearing: Double, distanceKm: Double): Pair<Double, Double> {
val earthRadius = 6378.1
val bearingR = Math.toRadians(bearing)
val latR = Math.toRadians(lat)
val lonR = Math.toRadians(lon)
val distanceToRadius = distanceKm / earthRadius
val newLatR = Math.asin(Math.sin(latR) * Math.cos(distanceToRadius) +
Math.cos(latR) * Math.sin(distanceToRadius) * Math.cos(bearingR))
val newLonR = lonR + Math.atan2(Math.sin(bearingR) * Math.sin(distanceToRadius) * Math.cos(latR),
Math.cos(distanceToRadius) - Math.sin(latR) * Math.sin(newLatR))
val latNew = Math.toDegrees(newLatR)
val lonNew = Math.toDegrees(newLonR)
return Pair(latNew, lonNew)
}
@mfilenko
Copy link

Thanks for posting this snippet! 👍

I was adopting it to my project and noticed that kotlin.math.* can be used instead of java.lang.Math for trigonometric functions 😉

@Mazur93
Copy link

Mazur93 commented Aug 5, 2021

Thank you very much for sharing! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment