0

I have two known locations and a distance to travel. The start location is: San Francisco lat 37.7576793, lon -122.5076404 The destination location is: Redwood City lat 37.5083527, lon -122.2856248 The distance is approximately 33.992 km How can I calculate the coordinates after 13km distance, traveled on a straight line?

 private fun distanceBetweenLocationsInKm(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double {
    val theta = lon1 - lon2
    var distance =
        Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(
            deg2rad(theta)
        )
    distance = Math.acos(distance)
    distance = rad2deg(distance)
    distance *= Constants.NUMBER_OF_MINUES_IN_A_DEGREE * Constants.STATUTE_MILES_IN_A_NAUTICAL_MILE
    distance *= Constants.ONE_MILE_IN_KM
    return distance
}

private fun deg2rad(deg: Double): Double {
    return deg * Math.PI / 180.0
}

private fun rad2deg(rad: Double): Double {
    return rad * 180.0 / Math.PI
}
melanzane
  • 483
  • 1
  • 6
  • 16
  • What exactly is wrong with your code? – Sweeper Dec 29 '21 at 13:24
  • I can't figure out how to calculate the coordinates after 13km have been traveled. – melanzane Dec 29 '21 at 13:26
  • I see it can be done with the geopy library in Python. Is there a similar one in Kotlin? – melanzane Dec 29 '21 at 13:29
  • The library they are using just does maths. It’s not really anything special to a particular language. It’s just a case of looking for the right equations. First you need to find a function to get a bearing between two coordinates. Then use the function from the above question to find the coordinates after travelling a given distance in the beating you worked out. Break it down to smaller problems and solve them one at a time. The answer given in the question I linked is just using maths. No library. – Fogmeister Dec 29 '21 at 13:31
  • Someone has even commented that they implemented it in kotlin with a link. – Fogmeister Dec 29 '21 at 13:33
  • Ok, it's easier than I thought :) Thanks! – melanzane Dec 29 '21 at 13:52

0 Answers0