Skip to content

Instantly share code, notes, and snippets.

@Zaherkorechi
Forked from vs-krasnov/geo.kt
Created April 12, 2022 12:11
Show Gist options
  • Save Zaherkorechi/2403bffc2256719c793f89023cbc9d24 to your computer and use it in GitHub Desktop.
Save Zaherkorechi/2403bffc2256719c793f89023cbc9d24 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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment