fetchRoute method

Future<void> fetchRoute()

Implementation

Future<void> fetchRoute() async {
  if (_mapboxMap == null || _currentPosition == null ||
      _destinationPosition == null || _routeService == null) {
    return;
  }

  _isLoadingRoute = true;
  notifyListeners();

  try {
    final route = await _routeService!.getRoute(
      _currentPosition!,
      _destinationPosition!,
    );

    if (route.points.isEmpty) {
      _showError('No route found');
      _isLoadingRoute = false;
      notifyListeners();
      return;
    }

    // Generate placeholder routes with different emphasis
    final performanceRoute = _generatePlaceholderRoute(route, RouteEmphasis.performance);
    final scenicRoute = _generatePlaceholderRoute(route, RouteEmphasis.scenic);

    // Store main route and alternatives
    _routeAlternatives = [route, performanceRoute, scenicRoute];

    // Display the route on the map
    await _routeService!.displayRoute(_mapboxMap!, route);

    // Use CameraController to fit route in view
    if (_cameraController != null) {
      await _cameraController!.fitRouteInView(route, padding: 50.0, pitch: 45.0);
    }

    _routeDetails = route;
    _routeOverviewMode = true;
    _isLoadingRoute = false;
    notifyListeners();
  } catch (e) {
    print('Error fetching route: $e');
    _showError('Unable to calculate route');
    _isLoadingRoute = false;
    notifyListeners();
  }
}