flutter_map icon indicating copy to clipboard operation
flutter_map copied to clipboard

[BUG] fitting bounds throws Infinity or NaN toInt or does not fit map correctly

Open typexy opened this issue 2 years ago • 6 comments

What is the bug?

when fitbounds is called, we have to distinguish 2 cases:

  1. multiple points are given -> zoom level is not calculated correctly, it shows whole map
  2. 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

typexy avatar Sep 20 '22 21:09 typexy

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.

ibrierley avatar Sep 21 '22 06:09 ibrierley

I noticed the same issue after upgrading to 3.0

FabianTerhorst avatar Sep 21 '22 14:09 FabianTerhorst

Can you provide some example settings (with latlngs etc) to reproduce the problem.

ibrierley avatar Sep 21 '22 14:09 ibrierley

i.e a minimal test example page that people can run that is.

ibrierley avatar Sep 21 '22 15:09 ibrierley

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.

Wackymax avatar Sep 25 '22 08:09 Wackymax

Seems like this should be fixed with https://github.com/fleaflet/flutter_map/pull/1369

Wackymax avatar Sep 25 '22 08:09 Wackymax

I encountered the same issue.

TitanKing avatar Oct 05 '22 14:10 TitanKing

Does it work with the fix (i.e latest Github release)?

ibrierley avatar Oct 05 '22 14:10 ibrierley

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.

TitanKing avatar Oct 07 '22 07:10 TitanKing

Maybe put some code up, it may depends if you are using other bounds parameters like maxBounds etc as well.

ibrierley avatar Oct 07 '22 08:10 ibrierley

Hi @TitanKing, any news? Hi @typexy @Wackymax, has the issue you were experiencing been fixed?

JaffaKetchup avatar Oct 10 '22 20:10 JaffaKetchup

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: @.***>

Wackymax avatar Oct 11 '22 05:10 Wackymax

Thanks, in that case I'm going to close this for now. Let me know if you need it reopened!

JaffaKetchup avatar Oct 11 '22 06:10 JaffaKetchup

I have the issue after updating to v3.0.0

shahpasandar avatar Dec 11 '22 08:12 shahpasandar

Could you see if the problem is still there with this PR.. here

ibrierley avatar Dec 11 '22 10:12 ibrierley

Could you see if the problem is still there with this PR.. here

I've tested that PR and the problem was resolved.

shahpasandar avatar Dec 11 '22 10:12 shahpasandar