From 322ca64c8286e9da5ffb5ac7ab632dda7ded910e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 17 Jun 2025 18:48:43 -0700 Subject: [PATCH] More optimal distance calculation --- lib/geolocation_service.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/geolocation_service.dart b/lib/geolocation_service.dart index 3714c8c..5e14259 100644 --- a/lib/geolocation_service.dart +++ b/lib/geolocation_service.dart @@ -93,8 +93,9 @@ class GeolocationService { const earthRadius = 6371008.8; // meters final dLat = _degToRad(to.coords.latitude - from.latitude); final dLon = _degToRad(to.coords.longitude - from.longitude); - final a = sin(dLat / 2) * sin(dLat / 2) + - cos(_degToRad(from.latitude)) * cos(_degToRad(to.coords.latitude)) * sin(dLon / 2) * sin(dLon / 2); + final sinLat = sin(dLat / 2); + final sinLon = sin(dLon / 2); + final a = sinLat * sinLat + cos(_degToRad(from.latitude)) * cos(_degToRad(to.coords.latitude)) * sinLon * sinLon; final c = 2 * atan2(sqrt(a), sqrt(1 - a)); return earthRadius * c; }