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

[google_maps_flutter]: LatLng longitude loses precision in constructor #90574 #4374

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
});
});
}