flutter_map
flutter_map copied to clipboard
[BUG] fitting bounds throws Infinity or NaN toInt or does not fit map correctly
What is the bug?
when fitbounds is called, we have to distinguish 2 cases:
- multiple points are given -> zoom level is not calculated correctly, it shows whole map
- one point is given -> Unsupported operation: Infinity or NaN toInt -> is thrown at following function:
/// Create new [CustomPoint] whose [x] and [y] values are rounded down /// to int CustomPoint<T> floor() { return CustomPoint<T>(x.floor(), y.floor()); }
What is the expected behaviour?
Expected behavior is to show a map centered at the middle of the given LatLng points and to not throw an error.
How can we reproduce this issue?
class Maps extends StatefulWidget {
final LatLng? initMarker;
final List<LatLng> markers;
Maps({Key? key, this.initMarker, required this.markers}) : super(key: key) {
var multipleTestMarkrs = [
LatLng(52.527194793354084, 13.371265245125935),
LatLng(48.14384446905119, 11.57992496759276)
];
var singleTestMarker = [LatLng(52.527194793354084, 13.371265245125935)];
this.markers = singleTestMarker;
}
@override
_MapsState createState() => _MapsState();
}
class _MapsState extends State<Maps> {
bool hasMarkers() {
return widget.markers.isNotEmpty;
}
@override
Widget build(BuildContext context) {
final options = MapOptions(
interactiveFlags: InteractiveFlag.none,
bounds: LatLngBounds.fromPoints(widget.markers),
center: computeCentroid(widget.markers),
maxBounds: LatLngBounds.fromPoints(widget.markers),
screenSize: MediaQuery.of(context).size,
boundsOptions: FitBoundsOptions(padding: EdgeInsets.all(20)));
return hasMarkers()
? Container(
height: 300,
child: IgnorePointer(
child: FlutterMap(
options: options,
children: [
TileLayer(
retinaMode:
true && MediaQuery.of(context).devicePixelRatio > 1.0,
urlTemplate:
"https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png",
subdomains: ['a', 'b', 'c'],
),
MarkerLayer(
markers: [
for (var marker in widget.markers)
Marker(
width: 80.0,
height: 80.0,
point: marker,
builder: (ctx) => const Icon(Icons.pin_drop,
color: StyleGuide.orange),
),
],
),
],
),
),
)
: Container(
decoration: const BoxDecoration(color: StyleGuide.orange),
child: const Text("NO MAP TO SHOW"),
);
}
LatLng computeCentroid(List<LatLng> points) {
double latitude = 0;
double longitude = 0;
int n = points.length;
for (LatLng point in points) {
latitude += point.latitude;
longitude += point.longitude;
}
return LatLng(latitude / n, longitude / n);
}
}
Do you have a potential solution?
No response
Can you provide any other information?
Unsupported operation: Infinity or NaN toInt
#0 double.floor (dart:core-patch/double.dart) #1 CustomPoint.floor (package:flutter_map/src/core/point.dart:22:29) #2 FlutterMapState.getPixelBounds (package:flutter_map/src/map/flutter_map_state.dart:593:47) #3 FlutterMapState.move (package:flutter_map/src/map/flutter_map_state.dart:457:20) #4 FlutterMapState.fitBounds (package:flutter_map/src/map/flutter_map_state.dart:490:5) #5 FlutterMapState.initState (package:flutter_map/src/map/flutter_map_state.dart:47:7) #6 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5015:57)
Platforms Affected
Android, iOS
Severity
Erroneous: Prevents normal functioning and causes errors in the console
Frequency
Consistently: Always occurs at the same time and location
Requirements
- [X] I agree to follow this project's Code of Conduct
- [X] My Flutter/Dart installation is unaltered, and
flutter doctor
finds no relevant issues - [X] I am using the latest stable version of this package
- [X] I have checked the FAQs section on the documentation website
- [X] I have checked for similar issues which may be duplicates
You could just add a check not to call fitBounds if you don't actually have any bounds....but ideally yes, there could be a protection /default behaviour in there (or simply do nothing).
I also note you are using center, bounds and maxBounds all together, which are potentially contradictory. You'd probably have to show example markers to really see the other issue when there are more than one markers/points.
I noticed the same issue after upgrading to 3.0
Can you provide some example settings (with latlngs etc) to reproduce the problem.
i.e a minimal test example page that people can run that is.
Getting the same problem, worked correctly before upgrading too 3.0.0. The provided example above should work too reproduce the issue if just passing the bounds and boundOptions.
Seems like this should be fixed with https://github.com/fleaflet/flutter_map/pull/1369
I encountered the same issue.
Does it work with the fix (i.e latest Github release)?
Does it work with the fix (i.e latest Github release)?
It does seem to fix the issue, however, I now noticed that when using the bounds paramater with LatLngBounds.fromPoints, it does not allow the map to be zoomed in or out. Might be another issue causing this, but it does happen my side.
Maybe put some code up, it may depends if you are using other bounds parameters like maxBounds etc as well.
Hi @TitanKing, any news? Hi @typexy @Wackymax, has the issue you were experiencing been fixed?
The code that is currently on main has solved the problems for me.On 10 Oct 2022, at 22:27, Luka S @.***> wrote: Hi @TitanKing, any news? Hi @typexy @Wackymax, has the issue you were experiencing been fixed?
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>
Thanks, in that case I'm going to close this for now. Let me know if you need it reopened!
I have the issue after updating to v3.0.0
Could you see if the problem is still there with this PR.. here
Could you see if the problem is still there with this PR.. here
I've tested that PR and the problem was resolved.