[google_maps_flutter]: LatLng longitude loses precision in constructor #90574 (#4374)
diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md b/packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md
index 464c33e..3a22dde 100644
--- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md
+++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 2.1.3
+
+* `LatLng` constructor maintains longitude precision when given within
+ acceptable range
+
## 2.1.2
* Add additional marker drag events
diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/location.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/location.dart
index 42c66e0..7a1aaf0 100644
--- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/location.dart
+++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/location.dart
@@ -14,13 +14,16 @@
/// The latitude is clamped to the inclusive interval from -90.0 to +90.0.
///
/// The longitude is normalized to the half-open interval from -180.0
- /// (inclusive) to +180.0 (exclusive)
+ /// (inclusive) to +180.0 (exclusive).
const LatLng(double latitude, double longitude)
: assert(latitude != null),
assert(longitude != null),
latitude =
(latitude < -90.0 ? -90.0 : (90.0 < latitude ? 90.0 : latitude)),
- longitude = (longitude + 180.0) % 360.0 - 180.0;
+ // Avoids normalization if possible to prevent unnecessary loss of precision
+ longitude = longitude >= -180 && longitude < 180
+ ? longitude
+ : (longitude + 180.0) % 360.0 - 180.0;
/// The latitude in degrees between -90.0 and 90.0, both inclusive.
final double latitude;
diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml b/packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml
index 2a2c9cf..d3d7653 100644
--- a/packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml
+++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml
@@ -4,7 +4,7 @@
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
-version: 2.1.2
+version: 2.1.3
environment:
sdk: '>=2.12.0 <3.0.0'
diff --git a/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/location_test.dart b/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/location_test.dart
new file mode 100644
index 0000000..80f6961
--- /dev/null
+++ b/packages/google_maps_flutter/google_maps_flutter_platform_interface/test/types/location_test.dart
@@ -0,0 +1,62 @@
+// Copyright 2013 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
+
+void main() {
+ TestWidgetsFlutterBinding.ensureInitialized();
+
+ group('LanLng constructor', () {
+ test('Maintains longitude precision if within acceptable range', () async {
+ const lat = -34.509981;
+ const lng = 150.792384;
+
+ final latLng = LatLng(lat, lng);
+
+ expect(latLng.latitude, equals(lat));
+ expect(latLng.longitude, equals(lng));
+ });
+
+ test('Normalizes longitude that is below lower limit', () async {
+ const lat = -34.509981;
+ const lng = -270.0;
+
+ final latLng = LatLng(lat, lng);
+
+ expect(latLng.latitude, equals(lat));
+ expect(latLng.longitude, equals(90.0));
+ });
+
+ test('Normalizes longitude that is above upper limit', () async {
+ const lat = -34.509981;
+ const lng = 270.0;
+
+ final latLng = LatLng(lat, lng);
+
+ expect(latLng.latitude, equals(lat));
+ expect(latLng.longitude, equals(-90.0));
+ });
+
+ test('Includes longitude set to lower limit', () async {
+ const lat = -34.509981;
+ const lng = -180.0;
+
+ final latLng = LatLng(lat, lng);
+
+ expect(latLng.latitude, equals(lat));
+ expect(latLng.longitude, equals(-180.0));
+ });
+
+ test('Normalizes longitude set to upper limit', () async {
+ const lat = -34.509981;
+ const lng = 180.0;
+
+ final latLng = LatLng(lat, lng);
+
+ expect(latLng.latitude, equals(lat));
+ expect(latLng.longitude, equals(-180.0));
+ });
+ });
+}