-
-
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.
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
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