Created
November 25, 2018 17:14
-
-
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.
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
Thanks for posting this snippet! 👍
I was adopting it to my project and noticed that
kotlin.math.*
can be used instead ofjava.lang.Math
for trigonometric functions 😉