Skip to content

Commit

Permalink
#267 feat : DistanceUtil 생성 haversine 공통 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
BENDENG1 committed Feb 8, 2024
1 parent 524594f commit d93bf61
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.avengers.presentation.util

import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.sqrt

object DistanceUtil {

fun haversineDistance(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double {
val radius = 6371000 // 지구 반지름(미터 단위)
val distanceLatitude = Math.toRadians(lat2 - lat1)
val distanceLongitude = Math.toRadians(lon2 - lon1)

val a = (sin(distanceLatitude / 2) * sin(distanceLatitude / 2)) +
(cos(Math.toRadians(lat1)) * cos(Math.toRadians(lat2)) *
sin(distanceLongitude / 2) * sin(distanceLongitude / 2))

val c = 2 * atan2(sqrt(a), sqrt(1 - a))
return radius * c
}
}

0 comments on commit d93bf61

Please sign in to comment.