getOptimalRoutesCenterViewport method
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 calculatedscreenRect: Target screen rectangle in physical pixels where routes should fit, relative to the map view's top-left corner. Ifnull, uses the entire viewport. Defaults tonull. 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:
- centerOnRoutes - Center on multiple routes
- centerOnAreaRect - Center area within viewport rectangle
- getOptimalHighlightCenterViewport - Calculate optimal viewport for highlights
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;
}