remove line after change

This commit is contained in:
Valentin CZERYBA 2024-11-17 11:31:07 +01:00
parent 7f5d59857c
commit c8223d9b7d

View File

@ -52,6 +52,7 @@ class _MapboxPagesState extends State<MapboxPages> with ShowErrorDialog {
bool isLoading = true;
late LatLng userPosition;
bool isUserPositionInitialized = false;
Line? currentRouteLine;
@override
void initState() {
@ -223,6 +224,11 @@ class _MapboxPagesState extends State<MapboxPages> with ShowErrorDialog {
}
Future<void> _drawRouteAndMarkers() async {
// Remove previous route line if it exists
if (currentRouteLine != null) {
await mapController.removeLine(currentRouteLine!);
currentRouteLine = null;
}
if (!isUserPositionInitialized) {
showErrorDialog(
context, "User position is not yet initialized. Try again.");
@ -249,10 +255,10 @@ class _MapboxPagesState extends State<MapboxPages> with ShowErrorDialog {
));
// Fetch and draw route
await _fetchRoute(userPosition!, destination, selectedMode);
await _fetchRoute(userPosition, destination, selectedMode);
if (routeCoordinates.isNotEmpty) {
await mapController.addLine(
currentRouteLine = await mapController.addLine(
LineOptions(
geometry: routeCoordinates,
lineColor: '#3b9ddd',
@ -260,6 +266,7 @@ class _MapboxPagesState extends State<MapboxPages> with ShowErrorDialog {
lineOpacity: 0.8,
),
);
_zoomToFitRoute(routeCoordinates);
}
} else {