import 'dart:convert'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:mapbox_gl/mapbox_gl.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; // Import dotenv import '../classes/alert.dart'; // Assuming this contains your error dialog code. import '../classes/events.dart'; // Your Event class, assuming you are using it. import '../variable/globals.dart' as globals; void main() async { await dotenv.load(fileName: ".env"); // Load .env file runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const Mapboxpages(title: 'Event Location'), ); } } class Mapboxpages extends StatefulWidget { const Mapboxpages({Key? key, required this.title}) : super(key: key); final String title; @override State createState() => _MapboxpagesState(); } class _MapboxpagesState extends State with ShowErrorDialog { late String mapboxAccessToken; late MapboxMapController mapController; double longitude = 0.0; double latitude = 0.0; bool isLoading = true; @override void initState() { super.initState(); _initToken(); } // Load the Mapbox access token from the .env file void _initToken() async { mapboxAccessToken = dotenv.env['MAPBOX_ACCESS_TOKEN'] ?? ''; if (mapboxAccessToken.isEmpty) { showErrorDialog(context, "Mapbox Access Token is not available."); return; } // Fetch event location using the title (address or name) await _fetchEventLocation(); // Set the state after fetching coordinates } // Fetch location coordinates using the event title Future _fetchEventLocation() async { if (widget.title.isNotEmpty && mapboxAccessToken.isNotEmpty) { final geocodeUrl = Uri.parse( 'https://api.mapbox.com/geocoding/v5/mapbox.places/${Uri.encodeComponent(widget.title)}.json?access_token=$mapboxAccessToken', ); final geocodeResponse = await http.get(geocodeUrl); if (geocodeResponse.statusCode == 200) { final geocodeData = json.decode(geocodeResponse.body); if (geocodeData['features'].isNotEmpty) { final coordinates = geocodeData['features'][0]['geometry']['coordinates']; longitude = coordinates[0]; // Longitude latitude = coordinates[1]; // Latitude setState(() { isLoading = false; }); } } else { showErrorDialog(context, "Failed to fetch location data."); } } } // Called when the map is created void _onMapCreated(MapboxMapController controller) { mapController = controller; // Log the map controller and coordinates print("Mapbox controller initialized: $mapController"); print("lat - long : $latitude - $longitude"); // Check if the mapController is really initialized if (mapController != null) { try { // Check if symbol options are correctly set final symbolOptions = SymbolOptions( geometry: LatLng(latitude, longitude), iconImage: "marker-15", // Make sure this icon exists in your assets ); // Debugging symbol options print("Adding symbol with options: $symbolOptions"); mapController.addSymbol(symbolOptions); } catch (e) { // Handle any exception that occurs when adding the symbol print("Error when adding symbol: $e"); } } else { print( "Error: MapboxMapController is null at the time of symbol addition"); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(widget.title)), body: isLoading ? Center(child: CircularProgressIndicator()) : MapboxMap( accessToken: mapboxAccessToken, // Your Mapbox API key onMapCreated: _onMapCreated, initialCameraPosition: CameraPosition( target: LatLng(latitude, longitude), zoom: 14.0, ), ), ); } }