From 35fc1cfa2528de6909ff538e792ec79f13c5aab6 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 4 Sep 2025 09:04:24 +0200 Subject: [PATCH] add zoomfit --- covas_mobile_new/lib/pages/MapboxPages.dart | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/covas_mobile_new/lib/pages/MapboxPages.dart b/covas_mobile_new/lib/pages/MapboxPages.dart index a5072b0..67941a9 100644 --- a/covas_mobile_new/lib/pages/MapboxPages.dart +++ b/covas_mobile_new/lib/pages/MapboxPages.dart @@ -168,6 +168,43 @@ class _MapboxPagesState extends State with ShowAlertDialog { } } + Future _zoomToFitRoute(List> coordinates) async { + if (mapboxMap == null || coordinates.isEmpty) return; + + double minLat = coordinates.first[1]; + double maxLat = coordinates.first[1]; + double minLng = coordinates.first[0]; + double maxLng = coordinates.first[0]; + + for (var coord in coordinates) { + if (coord[1] < minLat) minLat = coord[1]; + if (coord[1] > maxLat) maxLat = coord[1]; + if (coord[0] < minLng) minLng = coord[0]; + if (coord[0] > maxLng) maxLng = coord[0]; + } + + final bounds = mapbox.CoordinateBounds( + southwest: mapbox.Point(coordinates: mapbox.Position(minLng, minLat)), + northeast: mapbox.Point(coordinates: mapbox.Position(maxLng, maxLat)), + infiniteBounds: true); + + // Calculer une CameraOptions automatiquement à partir des bounds + final cameraOptions = await mapboxMap!.cameraForCoordinateBounds( + bounds, + mapbox.MbxEdgeInsets( + top: 50, left: 50, right: 50, bottom: 50), // marges + 0.0, + 0.0, + null, + null); + + // Appliquer la caméra avec animation + await mapboxMap!.flyTo( + cameraOptions, + mapbox.MapAnimationOptions(duration: 1000), + ); + } + Future _drawRouteAndMarkers() async { if (mapboxMap == null || !isUserPositionInitialized) return;