Skip to content

Commit 55c46b1

Browse files
committed
fix(mobile): handle geolocator -1 sentinel for unknown heading/speed/accuracy
1 parent df2c46d commit 55c46b1

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

‎mobile/lib/navigation/location_converter.dart‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import 'package:geolocator/geolocator.dart';
44
UserLocation positionToUserLocation(Position p) => UserLocation(
55
lat: p.latitude,
66
lng: p.longitude,
7-
horizontalAccuracyM: p.accuracy,
8-
courseDeg: p.heading > 0 ? p.heading : null,
9-
speedMps: p.speed,
7+
horizontalAccuracyM: p.accuracy >= 0 ? p.accuracy : 0,
8+
courseDeg: p.heading >= 0 ? p.heading : null,
9+
speedMps: p.speed >= 0 ? p.speed : null,
1010
timestampMs: p.timestamp.millisecondsSinceEpoch,
1111
);

‎mobile/test/navigation/location_converter_test.dart‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void main() {
2929
expect(result.timestampMs, 1000);
3030
});
3131

32-
test('sets courseDeg to null when heading is zero', () {
32+
test('preserves heading=0 (due north) as courseDeg 0.0', () {
3333
final pos = Position(
3434
latitude: 52.52,
3535
longitude: 13.405,
@@ -45,6 +45,26 @@ void main() {
4545

4646
final result = positionToUserLocation(pos);
4747

48+
expect(result.courseDeg, 0.0);
49+
});
50+
51+
test('sets courseDeg/speedMps to null when geolocator returns -1 sentinel', () {
52+
final pos = Position(
53+
latitude: 52.52,
54+
longitude: 13.405,
55+
accuracy: 5,
56+
heading: -1.0,
57+
speed: -1.0,
58+
timestamp: DateTime.fromMillisecondsSinceEpoch(0),
59+
altitude: 0,
60+
altitudeAccuracy: 0,
61+
headingAccuracy: 0,
62+
speedAccuracy: 0,
63+
);
64+
65+
final result = positionToUserLocation(pos);
66+
4867
expect(result.courseDeg, isNull);
68+
expect(result.speedMps, isNull);
4969
});
5070
}

0 commit comments

Comments
 (0)