getOptimalRoutesCenterViewport method

Rectangle<int> getOptimalRoutesCenterViewport(
  1. List<Route> routes, {
  2. Rectangle<int>? screenRect,
})

Calculates the optimal viewport for displaying multiple routes.

Returns an adjusted viewport rectangle that ensures all specified routes will be visible when used with centerOnAreaRect. The calculation accounts for route paths and automatically determines the best viewport dimensions to fit all routes within the specified screen rectangle.

This is useful for pre-calculating viewport dimensions before centering, particularly when implementing custom padding or UI constraints.

Parameters

  • routes: List of Route objects whose combined extent should be calculated
  • screenRect: Target screen rectangle in physical pixels where routes should fit, relative to the map view's top-left corner. If null, uses the entire viewport. Defaults to null. Useful for fitting within UI elements or adding margins

Returns

  • Optimal viewport Rectangle<int> in physical pixels that will fit all routes, relative to the map view's top-left corner

See also:

Implementation

Rectangle<int> getOptimalRoutesCenterViewport(
  final List<Route> routes, {
  final Rectangle<int>? screenRect,
}) {
  final OperationResult resultString = objectMethod(
    _pointerId,
    'MapView',
    'getOptimalRoutesCenterViewport',
    args: <String, dynamic>{
      'routesList': RouteList.fromList(routes).pointerId,
      if (screenRect != null)
        'viewRc': RectType<int>.fromRectangle(screenRect),
    },
  );

  final Rectangle<int> rect = Rectangle<int>(
    resultString['result']['x'] ?? 0,
    resultString['result']['y'] ?? 0,
    resultString['result']['width'] ?? 0,
    resultString['result']['height'] ?? 0,
  );

  return rect;
}