Skip to content

Commit

Permalink
fix: allowed LatLngBounds.center to work across world boundary & ad…
Browse files Browse the repository at this point in the history
…ded `simpleCenter` (#1860)
  • Loading branch information
monsieurtanuki authored May 26, 2024
1 parent 58ff560 commit 8114a16
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/src/geo/latlng_bounds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,15 @@ class LatLngBounds {
final lambda3 = lambda1 + atan2(by, cos(phi1) + bx);

// phi3 and lambda3 are actually in radians and LatLng wants degrees
return LatLng(phi3 * radians2Degrees, lambda3 * radians2Degrees);
return LatLng(
phi3 * radians2Degrees,
(lambda3 * radians2Degrees + 540) % 360 - 180,
);
}

/// Obtain simple coordinates of the bounds center
LatLng get simpleCenter => LatLng((south + north) / 2, (east + west) / 2);

/// Checks whether [point] is inside bounds
bool contains(LatLng point) =>
point.longitude >= west &&
Expand Down
47 changes: 47 additions & 0 deletions test/geo/latlng_bounds_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,52 @@ void main() {
expect(bounds1.hashCode, bounds2.hashCode);
});
});

group('center', () {
// cf. https://github.com/fleaflet/flutter_map/issues/1689
test('should calculate center point #1', () async {
final bounds = LatLngBounds(
const LatLng(-77.45, -171.16),
const LatLng(46.64, 25.88),
);
final center = bounds.center;
expect(center.latitude, greaterThanOrEqualTo(-90));
expect(center.latitude, lessThanOrEqualTo(90));
expect(center.longitude, greaterThanOrEqualTo(-180));
expect(center.longitude, lessThanOrEqualTo(180));
});
test('should calculate center point #2', () async {
final bounds = LatLngBounds(
const LatLng(-0.87, -179.86),
const LatLng(84.92, 23.86),
);
final center = bounds.center;
expect(center.latitude, greaterThanOrEqualTo(-90));
expect(center.latitude, lessThanOrEqualTo(90));
expect(center.longitude, greaterThanOrEqualTo(-180));
expect(center.longitude, lessThanOrEqualTo(180));
});
});

group('simpleCenter', () {
test('should calculate center point #1', () async {
final bounds = LatLngBounds(
const LatLng(-77.45, -171.16),
const LatLng(46.64, 25.88),
);
final center = bounds.simpleCenter;
expect(center.latitude, (-77.45 + 46.64) / 2);
expect(center.longitude, (-171.16 + 25.88) / 2);
});
test('should calculate center point #2', () async {
final bounds = LatLngBounds(
const LatLng(-0.87, -179.86),
const LatLng(84.92, 23.86),
);
final center = bounds.simpleCenter;
expect(center.latitude, (-0.87 + 84.92) / 2);
expect(center.longitude, (-179.86 + 23.86) / 2);
});
});
});
}

0 comments on commit 8114a16

Please sign in to comment.