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