forked from yy0ung/nibobnebob
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#267 feat : DistanceUtil 생성 haversine 공통 사용
- Loading branch information
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
Aos/presentation/src/main/java/com/avengers/presentation/util/DistanceUtil.kt
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
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 | ||
} | ||
} |