Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 7ef23db

Browse files
committed
#90574: Avoids normalizing longitude in LatLng constructor if value is already within range to avoid unnecessary precision loss
1 parent 6a2e738 commit 7ef23db

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/location.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class LatLng {
2121
assert(longitude != null),
2222
latitude =
2323
(latitude < -90.0 ? -90.0 : (90.0 < latitude ? 90.0 : latitude)),
24-
longitude = longitude >= -180 || longitude < 180
24+
longitude = longitude >= -180 && longitude < 180
2525
? longitude
2626
: (longitude + 180.0) % 360.0 - 180.0;
2727

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2021 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
7+
8+
void main() {
9+
TestWidgetsFlutterBinding.ensureInitialized();
10+
11+
group('LanLng constructor', () {
12+
test('Maintains longitude precision if within acceptable range', () async {
13+
const lat = -34.509981;
14+
const lng = 150.792384;
15+
16+
final latLng = LatLng(lat, lng);
17+
18+
expect(latLng.latitude, equals(lat));
19+
expect(latLng.longitude, equals(lng));
20+
});
21+
22+
test('Normalizes longitude that is below lower limit', () async {
23+
const lat = -34.509981;
24+
const lng = -270.0;
25+
26+
final latLng = LatLng(lat, lng);
27+
28+
expect(latLng.latitude, equals(lat));
29+
expect(latLng.longitude, equals(90.0));
30+
});
31+
32+
test('Normalizes longitude that is above upper limit', () async {
33+
const lat = -34.509981;
34+
const lng = 270.0;
35+
36+
final latLng = LatLng(lat, lng);
37+
38+
expect(latLng.latitude, equals(lat));
39+
expect(latLng.longitude, equals(-90.0));
40+
});
41+
42+
test('Includes longitude set to lower limit', () async {
43+
const lat = -34.509981;
44+
const lng = -180.0;
45+
46+
final latLng = LatLng(lat, lng);
47+
48+
expect(latLng.latitude, equals(lat));
49+
expect(latLng.longitude, equals(-180.0));
50+
});
51+
52+
test('Normalizes longitude set to upper limit', () async {
53+
const lat = -34.509981;
54+
const lng = 180.0;
55+
56+
final latLng = LatLng(lat, lng);
57+
58+
expect(latLng.latitude, equals(lat));
59+
expect(latLng.longitude, equals(-180.0));
60+
});
61+
});
62+
}

0 commit comments

Comments
 (0)