Skip to content

Commit

Permalink
[google_maps_flutter]: LatLng longitude loses precision in constructo…
Browse files Browse the repository at this point in the history
…r #90574 (flutter#4374)
  • Loading branch information
Murray-Meller authored and amantoux committed Dec 11, 2021
1 parent 588e89b commit 003d568
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ class LatLng {
/// 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/google_maps_
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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
});
});
}

0 comments on commit 003d568

Please sign in to comment.