Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kotlin.math.*
- fun calculateHaversineDistance(
- lat1: Double, lon1: Double,
- lat2: Double, lon2: Double
- ): Double {
- val R = 6371 // Radius of the Earth in kilometers
- val dLat = Math.toRadians(lat2 - lat1)
- val dLon = Math.toRadians(lon2 - lon1)
- val a = sin(dLat / 2) * sin(dLat / 2) +
- cos(Math.toRadians(lat1)) * cos(Math.toRadians(lat2)) *
- sin(dLon / 2) * sin(dLon / 2)
- val c = 2 * atan2(sqrt(a), sqrt(1 - a))
- return R * c // Distance in kilometers
- }
- fun main() {
- // Example coordinates for two points
- val lat1 = 37.7749 // Latitude for point 1
- val lon1 = -122.4194 // Longitude for point 1
- val lat2 = 34.0522 // Latitude for point 2
- val lon2 = -118.2437 // Longitude for point 2
- val distance = calculateHaversineDistance(lat1, lon1, lat2, lon2)
- println("Distance between the two points: $distance kilometers")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement