diff --git a/covas_mobile/lib/pages/MapboxPages.dart b/covas_mobile/lib/pages/MapboxPages.dart index d0f643f..22df91e 100644 --- a/covas_mobile/lib/pages/MapboxPages.dart +++ b/covas_mobile/lib/pages/MapboxPages.dart @@ -50,14 +50,14 @@ class _MapboxPagesState extends State with ShowErrorDialog { double longitude = 0.0; double latitude = 0.0; bool isLoading = true; - LatLng? userPosition; + late LatLng userPosition; + bool isUserPositionInitialized = false; @override void initState() { super.initState(); _initToken(); _getEventInfo(); - _getUserLocation(); } void _initToken() { @@ -80,6 +80,7 @@ class _MapboxPagesState extends State with ShowErrorDialog { var events = jsonDecode(utf8.decode(responseGet.bodyBytes)); latitude = events["latitude"]; longitude = events["longitude"]; + _getUserLocation(); setState(() { isLoading = false; }); @@ -151,6 +152,7 @@ class _MapboxPagesState extends State with ShowErrorDialog { ); setState(() { userPosition = LatLng(position.latitude, position.longitude); + isUserPositionInitialized = true; }); } catch (e) { showErrorDialog(context, "Failed to get user location: $e"); @@ -193,11 +195,11 @@ class _MapboxPagesState extends State with ShowErrorDialog { final userMarkerImage = await _loadMarkerImage('images/marker.png'); // Register the image with Mapbox - await mapController.addImage('user-marker', userMarkerImage); + await mapController.addImage('event-marker', userMarkerImage); final symbolOptions = SymbolOptions( geometry: LatLng(latitude, longitude), - iconImage: "user-marker", // Use the registered custom marker + iconImage: "event-marker", // Use the registered custom marker iconSize: 0.4, // Optional: Adjust size ); @@ -220,6 +222,12 @@ class _MapboxPagesState extends State with ShowErrorDialog { } Future _drawRouteAndMarkers() async { + if (!isUserPositionInitialized) { + showErrorDialog( + context, "User position is not yet initialized. Try again."); + return; + } + if (mapController != null && userPosition != null && latitude != 0.0 && @@ -232,10 +240,10 @@ class _MapboxPagesState extends State with ShowErrorDialog { final userMarkerImage = await _loadMarkerImage('images/marker.png'); // Register the image with Mapbox - await mapController.addImage('event-marker', userMarkerImage); + await mapController.addImage('user-marker', userMarkerImage); await mapController.addSymbol(SymbolOptions( - geometry: destination, - iconImage: 'event-marker', // Custom icon for event + geometry: userPosition, + iconImage: 'user-marker', // Custom icon for event iconSize: 0.4, ));