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

Commit d9490e7

Browse files
[google_maps_flutter]: LatLng longitude loses precision in constructor #90574 (#4374)
1 parent f6d93a7 commit d9490e7

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

packages/google_maps_flutter/google_maps_flutter_platform_interface/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.1.3
2+
3+
* `LatLng` constructor maintains longitude precision when given within
4+
acceptable range
5+
16
## 2.1.2
27

38
* Add additional marker drag events

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ class LatLng {
1414
/// The latitude is clamped to the inclusive interval from -90.0 to +90.0.
1515
///
1616
/// The longitude is normalized to the half-open interval from -180.0
17-
/// (inclusive) to +180.0 (exclusive)
17+
/// (inclusive) to +180.0 (exclusive).
1818
const LatLng(double latitude, double longitude)
1919
: assert(latitude != null),
2020
assert(longitude != null),
2121
latitude =
2222
(latitude < -90.0 ? -90.0 : (90.0 < latitude ? 90.0 : latitude)),
23-
longitude = (longitude + 180.0) % 360.0 - 180.0;
23+
// Avoids normalization if possible to prevent unnecessary loss of precision
24+
longitude = longitude >= -180 && longitude < 180
25+
? longitude
26+
: (longitude + 180.0) % 360.0 - 180.0;
2427

2528
/// The latitude in degrees between -90.0 and 90.0, both inclusive.
2629
final double latitude;

packages/google_maps_flutter/google_maps_flutter_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/google_maps_
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.1.2
7+
version: 2.1.3
88

99
environment:
1010
sdk: '>=2.12.0 <3.0.0'
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2013 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)