add messae error

This commit is contained in:
Valentin CZERYBA 2024-11-17 10:46:17 +01:00
parent f880ac1002
commit 4a04520800

View File

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