fitRouteInView method

Future<void> fitRouteInView(
  1. RouteDetails route, {
  2. double padding = 50.0,
  3. double pitch = 0.0,
  4. double bearing = 0.0,
  5. int duration = 1500,
})

Fits the camera view to show an entire route

Implementation

Future<void> fitRouteInView(RouteDetails route, {
  double padding = 50.0,
  double pitch = 0.0,
  double bearing = 0.0,
  int duration = 1500,
}) async {
  if (route.points.isEmpty) return;

  try {
    // Mark animation starting
    _isCameraAnimating = true;

    final bounds = route.bounds;
    final southwest = mapbox.Point(
      coordinates: mapbox.Position(bounds.southwest.lng, bounds.southwest.lat)
    );
    final northeast = mapbox.Point(
      coordinates: mapbox.Position(bounds.northeast.lng, bounds.northeast.lat)
    );

    final coordinateBounds = mapbox.CoordinateBounds(
      southwest: southwest,
      northeast: northeast,
      infiniteBounds: false,
    );

    // Create padding
    final paddingEdges = mapbox.MbxEdgeInsets(
      top: padding,
      left: padding,
      bottom: padding,
      right: padding
    );

    final cameraOptions = await mapboxMap.cameraForCoordinateBounds(
      coordinateBounds,
      paddingEdges,
      bearing,
      pitch,
      null, // minZoom
      null  // maxZoom
    );

    await mapboxMap.flyTo(
      cameraOptions,
      mapbox.MapAnimationOptions(duration: duration),
    );

    // Mark animation complete
    _isCameraAnimating = false;
  } catch (e) {
    print('Error fitting route in view: $e');
    _isCameraAnimating = false; // Ensure flag is reset on error
    rethrow;
  }
}