diff --git a/lib/src/geo/latlng_bounds.dart b/lib/src/geo/latlng_bounds.dart index ad4a442dc..62724972f 100644 --- a/lib/src/geo/latlng_bounds.dart +++ b/lib/src/geo/latlng_bounds.dart @@ -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 && diff --git a/test/geo/latlng_bounds_test.dart b/test/geo/latlng_bounds_test.dart index 2509b0f60..20c009c99 100644 --- a/test/geo/latlng_bounds_test.dart +++ b/test/geo/latlng_bounds_test.dart @@ -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); + }); + }); }); }