From de9d6493bc7580279ccd5f09e4bc9e7c1b4d8a27 Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Wed, 30 Oct 2019 10:12:14 +0100 Subject: [PATCH] LatLng: fix distanceTo to correctly return meters --- .../com/sygic/travel/sdk/places/model/geo/LatLng.kt | 2 +- .../sygic/travel/sdk/places/model/geo/LatLngTest.kt | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sygic/travel/sdk/places/model/geo/LatLng.kt b/src/main/java/com/sygic/travel/sdk/places/model/geo/LatLng.kt index 938e02af..1a749881 100644 --- a/src/main/java/com/sygic/travel/sdk/places/model/geo/LatLng.kt +++ b/src/main/java/com/sygic/travel/sdk/places/model/geo/LatLng.kt @@ -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 { diff --git a/src/test/java/com/sygic/travel/sdk/places/model/geo/LatLngTest.kt b/src/test/java/com/sygic/travel/sdk/places/model/geo/LatLngTest.kt index 41566564..0e4d7a9b 100644 --- a/src/test/java/com/sygic/travel/sdk/places/model/geo/LatLngTest.kt +++ b/src/test/java/com/sygic/travel/sdk/places/model/geo/LatLngTest.kt @@ -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 @@ -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()) + } }