From 249d047e238df95d8a62189d5c3245120bc4421d Mon Sep 17 00:00:00 2001 From: patrickpoon-ucberkeley <52478489+patrickpoon-ucberkeley@users.noreply.github.com> Date: Fri, 9 Oct 2020 22:03:47 -0400 Subject: [PATCH] Bugfix: isPointWithinRadius false even if true Because this function calls getDistance() without an accuracy value, it rounds up to the nearest meter. --- src/isPointWithinRadius.test.js | 10 ++++++++++ src/isPointWithinRadius.ts | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/isPointWithinRadius.test.js b/src/isPointWithinRadius.test.js index 3482485..6ebbbc3 100644 --- a/src/isPointWithinRadius.test.js +++ b/src/isPointWithinRadius.test.js @@ -20,4 +20,14 @@ describe('isPointWithinRadius', () => { ) ).toBe(false); }); + + it('should return true if a given point is within a certain radius with high accuracy', () => { + expect( + isPointWithinRadius( + { latitude: 42.53098, longitude: -71.28029 }, + { latitude: 42.53101, longitude: -71.2803986 }, + 10 + ) + ).toBe(true); + }); }); diff --git a/src/isPointWithinRadius.ts b/src/isPointWithinRadius.ts index dbe3fe6..532907f 100644 --- a/src/isPointWithinRadius.ts +++ b/src/isPointWithinRadius.ts @@ -7,7 +7,8 @@ const isPointWithinRadius = ( center: GeolibInputCoordinates, radius: number ) => { - return getDistance(point, center) < radius; + const accuracy = 0.01; + return getDistance(point, center, accuracy) < radius; }; export default isPointWithinRadius;