Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allowed LatLngBounds.center to work across world boundary & added simpleCenter #1860

Merged
merged 6 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/src/geo/latlng_bounds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,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,
josxha marked this conversation as resolved.
Show resolved Hide resolved
);
}

/// 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);
});
});
});
}
Loading