flyToLocation method

Future<void> flyToLocation(
  1. RaliPosition position, {
  2. double zoom = 14.0,
  3. double pitch = 45.0,
  4. double bearing = 0.0,
  5. int duration = 1000,
})

//////////// //////////// Flies the camera to a specific location with customizable parameters

Implementation

// I.C - Camera Movement Methods
///////////////
/// Flies the camera to a specific location with customizable parameters
Future<void> flyToLocation(RaliPosition position, {
  double zoom = 14.0,
  double pitch = 45.0,
  double bearing = 0.0,
  int duration = 1000, // Reduced default duration for smoother tracking
}) async {
  try {
    // Throttle updates to prevent too many animations stacking
    final now = DateTime.now();
    if (now.difference(_lastCameraUpdate).inMilliseconds < 100) {
      // Skip this update if too soon after the last one
      return;
    }
    _lastCameraUpdate = now;

    // Mark animation starting
    _isCameraAnimating = true;

    final mapboxPosition = mapbox.Position(position.lng, position.lat);
    final point = mapbox.Point(coordinates: mapboxPosition);

    final camera = mapbox.CameraOptions(
      center: point,
      zoom: zoom,
      bearing: bearing,
      pitch: pitch,
    );

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

    // Mark animation complete
    _isCameraAnimating = false;
  } catch (e) {
    print('Error moving camera: $e');
    _isCameraAnimating = false; // Ensure flag is reset on error

    // Attempt direct camera update as fallback
    try {
      await mapboxMap.setCamera(
        mapbox.CameraOptions(
          center: mapbox.Point(
            coordinates: mapbox.Position(position.lng, position.lat)
          ),
          zoom: zoom,
          bearing: bearing,
          pitch: pitch,
        )
      );
    } catch (e2) {
      print('Fallback camera update also failed: $e2');
    }
  }
}