Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
5 tasks done
typexy opened this issue Sep 20, 2022 · 16 comments
Closed
5 tasks done
Labels
bug This issue reports broken functionality or another error

Comments

@typexy
Copy link

typexy commented Sep 20, 2022

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 floor() {
return CustomPoint(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

  • I agree to follow this project's Code of Conduct
  • My Flutter/Dart installation is unaltered, and flutter doctor finds no relevant issues
  • I am using the latest stable version of this package
  • I have checked the FAQs section on the documentation website
  • I have checked for similar issues which may be duplicates
@typexy typexy added bug This issue reports broken functionality or another error needs triage This new bug report needs reproducing and prioritizing labels Sep 20, 2022
@typexy typexy changed the title Fitbounds throws Infinity or NaN toInt or does not fit map correctly [BUG] Fitbounds throws Infinity or NaN toInt or does not fit map correctly Sep 20, 2022
@typexy typexy changed the title [BUG] Fitbounds throws Infinity or NaN toInt or does not fit map correctly [BUG] fitting bounds throws Infinity or NaN toInt or does not fit map correctly Sep 20, 2022
@ibrierley
Copy link
Collaborator

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.

@FabianTerhorst
Copy link

I noticed the same issue after upgrading to 3.0

@ibrierley
Copy link
Collaborator

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

@ibrierley
Copy link
Collaborator

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

@Wackymax
Copy link

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
Copy link

Seems like this should be fixed with #1369

@TitanKing
Copy link

I encountered the same issue.

@ibrierley
Copy link
Collaborator

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

@TitanKing
Copy link

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.

@ibrierley
Copy link
Collaborator

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

@JaffaKetchup
Copy link
Member

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

@JaffaKetchup JaffaKetchup added non-fatal and removed needs triage This new bug report needs reproducing and prioritizing labels Oct 10, 2022
@Wackymax
Copy link

Wackymax commented Oct 11, 2022 via email

@JaffaKetchup
Copy link
Member

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

@shahpasandar
Copy link

I have the issue after updating to v3.0.0

@ibrierley
Copy link
Collaborator

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

@shahpasandar
Copy link

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue reports broken functionality or another error
Projects
None yet
Development

No branches or pull requests

7 participants