Skip to content

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

@typexy

Description

@typexy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue reports broken functionality or another error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions