Skip to content

Commit a6d6e24

Browse files
committed
feat: Improve equality comparison for LocationCoordinate with near-equal precision
1 parent 3e384d4 commit a6d6e24

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

packages/stream_core/lib/src/query/filter/location/location_coordinate.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ class LocationCoordinate {
3737
@override
3838
bool operator ==(Object other) {
3939
if (identical(this, other)) return true;
40-
return other is LocationCoordinate &&
41-
runtimeType == other.runtimeType &&
42-
latitude == other.latitude &&
43-
longitude == other.longitude;
40+
if (other is! LocationCoordinate) return false;
41+
42+
const epsilon = 1e-7; // ~1cm precision
43+
final absLatDiff = (latitude - other.latitude).abs();
44+
final absLngDiff = (longitude - other.longitude).abs();
45+
46+
return absLatDiff < epsilon && absLngDiff < epsilon;
4447
}
4548

4649
@override
@@ -59,10 +62,8 @@ class LocationCoordinate {
5962
/// print('Distance: ${distance.inKilometers} km'); // ~343.56 km
6063
/// ```
6164
Distance distanceTo(LocationCoordinate other) {
62-
// If the coordinates are identical, the distance is zero.
63-
if (latitude == other.latitude && longitude == other.longitude) {
64-
return 0.meters;
65-
}
65+
// If the coordinates are equal, the distance is zero.
66+
if (this == other) return 0.meters;
6667

6768
const earthRadius = 6378137.0;
6869

packages/stream_core/test/query/location_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ void main() {
4949
expect(location1, isNot(equals(location3)));
5050
expect(location1.hashCode, equals(location2.hashCode));
5151
});
52+
53+
test('should consider near-equal coordinates as equal', () {
54+
const location1 = LocationCoordinate(
55+
latitude: 37.7749,
56+
longitude: -122.4194,
57+
);
58+
59+
// Coordinates differ by less than epsilon (~1cm)
60+
const location2 = LocationCoordinate(
61+
latitude: 37.77490000001,
62+
longitude: -122.41940000001,
63+
);
64+
65+
// Coordinates differ by more than epsilon
66+
const location3 = LocationCoordinate(
67+
latitude: 37.7749001,
68+
longitude: -122.4194001,
69+
);
70+
71+
expect(location1, equals(location2));
72+
expect(location1, isNot(equals(location3)));
73+
});
5274
});
5375

5476
group('Distance', () {

0 commit comments

Comments
 (0)