Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
LatLng: fix distanceTo to correctly return meters
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Oct 30, 2019
1 parent 00f6da1 commit de9d649
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ data class LatLng(
val lon2 = point.lng
val p = PI / 180
val a = 0.5 - cos((lat2 - lat1) * p) / 2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
return 12742 * asin(sqrt(a))
return 12742 * asin(sqrt(a)) * 1000
}

fun withPrecision(decimals: Int = 6): LatLng {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.sygic.travel.sdk.places.model.geo

import com.sygic.travel.sdk.places.model.geo.LatLng
import org.junit.Assert.assertEquals
import org.junit.Test
import kotlin.math.roundToInt

class LatLngTest {
@Test
Expand All @@ -24,4 +24,12 @@ class LatLngTest {
latLng.withPrecision(10).toApiExpression()
)
}

@Test
fun testDistance() {
val from = LatLng(49.233461, 16.572517)
val to = LatLng(49.231109, 16.573447)

assertEquals(270, from.distanceTo(to).roundToInt())
}
}

0 comments on commit de9d649

Please sign in to comment.