computeInitialCamera static method

CameraPosition computeInitialCamera(
  1. List<RoutePoint> points, {
  2. CameraPosition fallback = _fallbackPosition,
})

Returns a CameraPosition centered on points with an appropriate zoom level based on how spread out the points are.

Falls back to fallback if points is empty.

Implementation

static CameraPosition computeInitialCamera(List<RoutePoint> points,
    {CameraPosition fallback = _fallbackPosition}) {
  if (points.isEmpty) return fallback;

  final bounds = computeBounds(points);
  final centerLat =
      (bounds.southwest.latitude + bounds.northeast.latitude) / 2;
  final centerLng =
      (bounds.southwest.longitude + bounds.northeast.longitude) / 2;
  final latSpan = bounds.northeast.latitude - bounds.southwest.latitude;
  final lngSpan = bounds.northeast.longitude - bounds.southwest.longitude;
  final maxSpan = latSpan > lngSpan ? latSpan : lngSpan;

  ///calculate the zoom value based on the max span (center of the points)
  final zoom = switch (maxSpan) {
    > 0.5 => 10.0,
    > 0.2 => 11.0,
    > 0.1 => 12.0,
    > 0.05 => 13.0,
    > 0.01 => 14.0,
    _ => 15.0,
  };

  return CameraPosition(target: LatLng(centerLat, centerLng), zoom: zoom);
}