From 1603adf5dd93bbdd8ffb21cc563e739fb26b7af9 Mon Sep 17 00:00:00 2001 From: Winston Lowe Date: Wed, 11 Feb 2026 19:26:03 -0800 Subject: [PATCH] Add snapToGridCenter method to align position coordinates to grid --- lib/services/sparse_location_logger.dart | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/services/sparse_location_logger.dart b/lib/services/sparse_location_logger.dart index 08aacd9..5b8ae62 100644 --- a/lib/services/sparse_location_logger.dart +++ b/lib/services/sparse_location_logger.dart @@ -186,6 +186,37 @@ class SparseLocationLogger { } } + Position snapToGridCenter({ + required Position position, + required double cellSizeDegrees, // e.g. 0.01 ≈ 1.1 km, 0.001 ≈ 110 m + }) { + Position snappedPosition = position; + // Snap latitude + final latFloor = + (position.latitude / cellSizeDegrees).floor() * cellSizeDegrees; + final snappedLat = latFloor + (cellSizeDegrees / 2); + + // Snap longitude + final lonFloor = + (position.longitude / cellSizeDegrees).floor() * cellSizeDegrees; + final snappedLon = lonFloor + (cellSizeDegrees / 2); + + snappedPosition = Position( + latitude: snappedLat, + longitude: snappedLon, + altitude: position.altitude, + accuracy: position.accuracy, + heading: position.heading, + speed: position.speed, + speedAccuracy: position.speedAccuracy, + altitudeAccuracy: position.altitudeAccuracy, + headingAccuracy: position.headingAccuracy, + timestamp: position.timestamp, + ); + + return snappedPosition; + } + Future getGpxFilePath() async => _gpxFile?.path ?? 'Not started'; bool isLogging() => _positionStream != null; int getPointCount() => _currentSegment.trkpts.length;