Skip to content

Commit fdc089a

Browse files
fix: prevent throwing when LatLng is non-finite (#2182)
1 parent 097f79a commit fdc089a

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

lib/src/geo/crs.dart

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ abstract class Crs {
5353

5454
/// Similar to [latLngToXY] but converts the XY coordinates to an [Offset].
5555
Offset latLngToOffset(LatLng latlng, double zoom) {
56-
final (x, y) = latLngToXY(latlng, scale(zoom));
56+
final (x, y) = latLngToXY(checkLatLng(latlng), scale(zoom));
5757
return Offset(x, y);
5858
}
5959

@@ -72,6 +72,17 @@ abstract class Crs {
7272
/// Whether this CRS supports repeating worlds: repeated (feature) layers and
7373
/// unbounded horizontal scrolling along the longitude axis
7474
bool get replicatesWorldLongitude => false;
75+
76+
/// Throws if [latlng] is not finite (e.g. either NaN or infinite), which may
77+
/// cause memory leak.
78+
/// cf. https://github.com/fleaflet/flutter_map/issues/2178
79+
@protected
80+
LatLng checkLatLng(LatLng latlng) {
81+
if (!(latlng.latitude.isFinite && latlng.longitude.isFinite)) {
82+
throw Exception('LatLng is not finite: $latlng');
83+
}
84+
return latlng;
85+
}
7586
}
7687

7788
/// Internal base class for CRS with a single zoom-level independent transformation.
@@ -104,7 +115,7 @@ abstract class CrsWithStaticTransformation extends Crs {
104115

105116
@override
106117
(double, double) latLngToXY(LatLng latlng, double scale) {
107-
final (x, y) = projection.projectXY(latlng);
118+
final (x, y) = projection.projectXY(checkLatLng(latlng));
108119
return _transformation.transform(x, y, scale);
109120
}
110121

@@ -164,15 +175,18 @@ class Epsg3857 extends CrsWithStaticTransformation {
164175
);
165176

166177
@override
167-
(double, double) latLngToXY(LatLng latlng, double scale) =>
168-
_transformation.transform(
169-
SphericalMercator.projectLng(latlng.longitude),
170-
SphericalMercator.projectLat(latlng.latitude),
171-
scale,
172-
);
178+
(double, double) latLngToXY(LatLng latlng, double scale) {
179+
checkLatLng(latlng);
180+
return _transformation.transform(
181+
SphericalMercator.projectLng(latlng.longitude),
182+
SphericalMercator.projectLat(latlng.latitude),
183+
scale,
184+
);
185+
}
173186

174187
@override
175188
Offset latLngToOffset(LatLng latlng, double zoom) {
189+
checkLatLng(latlng);
176190
final (x, y) = _transformation.transform(
177191
SphericalMercator.projectLng(latlng.longitude),
178192
SphericalMercator.projectLat(latlng.latitude),
@@ -276,7 +290,7 @@ class Proj4Crs extends Crs {
276290
/// map point.
277291
@override
278292
(double, double) latLngToXY(LatLng latlng, double scale) {
279-
final (x, y) = projection.projectXY(latlng);
293+
final (x, y) = projection.projectXY(checkLatLng(latlng));
280294
final transformation = _getTransformationByZoom(zoom(scale));
281295
return transformation.transform(x, y, scale);
282296
}

0 commit comments

Comments
 (0)