fitBounds method

  1. @override
void fitBounds(
  1. List<LatLng> points
)
override

Moves the camera to fit points.

Implementations whose provider reports no reason for a camera change should mix in MapMoveAttribution and call beginProgrammaticMove() immediately before the platform call, so the move it produces is not reported to the host as a user gesture. See MapMoveSource.

Implementation

@override
void fitBounds(List<latlong.LatLng> points) async {
  if (points.length < 2) return;
  var firstLatLng = points.first;
  var s = firstLatLng.latitude,
      n = firstLatLng.latitude,
      w = firstLatLng.longitude,
      e = firstLatLng.longitude;
  for (var i = 1; i < points.length; i++) {
    var latlng = points[i];
    s = min(s, latlng.latitude);
    n = max(n, latlng.latitude);
    w = min(w, latlng.longitude);
    e = max(e, latlng.longitude);
  }
  final bounds =
      LatLngBounds(southwest: LatLng(s, w), northeast: LatLng(n, e));
  await _withMap((map) {
    // Claimed here rather than at the top of the method: everything above is
    // arithmetic, and `_withMap` can park for a map that does not exist yet.
    // Neither is time the camera is moving, and both would eat the window
    // that ends up attributing this move.
    beginProgrammaticMove();
    return map.animateCamera(
      CameraUpdate.newLatLngBounds(
        bounds,
        max(padding.left,
            max(padding.top, max(padding.right, padding.bottom))),
      ),
    );
  });
}