diff --git a/covas_mobile/lib/pages/MapboxPages.dart b/covas_mobile/lib/pages/MapboxPages.dart index b5741a3..bfa8049 100644 --- a/covas_mobile/lib/pages/MapboxPages.dart +++ b/covas_mobile/lib/pages/MapboxPages.dart @@ -1,6 +1,7 @@ import 'dart:convert'; +import 'dart:typed_data'; import 'dart:io'; - +import 'package:flutter/services.dart'; // For loading assets import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:mapbox_gl/mapbox_gl.dart'; @@ -41,7 +42,7 @@ class Mapboxpages extends StatefulWidget { class _MapboxpagesState extends State with ShowErrorDialog { late String mapboxAccessToken; - late MapboxMapController mapController; + MapboxMapController? mapController; // Mark mapController as nullable double longitude = 0.0; double latitude = 0.0; bool isLoading = true; @@ -62,8 +63,6 @@ class _MapboxpagesState extends State with ShowErrorDialog { // Fetch event location using the title (address or name) await _fetchEventLocation(); - - // Set the state after fetching coordinates } // Fetch location coordinates using the event title @@ -80,11 +79,14 @@ class _MapboxpagesState extends State with ShowErrorDialog { if (geocodeData['features'].isNotEmpty) { final coordinates = geocodeData['features'][0]['geometry']['coordinates']; + print("geodate : ${geocodeData['features'][0]}"); longitude = coordinates[0]; // Longitude latitude = coordinates[1]; // Latitude setState(() { isLoading = false; }); + } else { + showErrorDialog(context, "Location not found."); } } else { showErrorDialog(context, "Failed to fetch location data."); @@ -92,8 +94,14 @@ class _MapboxpagesState extends State with ShowErrorDialog { } } + // Load image from assets as Uint8List + Future _loadMarkerImage() async { + final ByteData data = await rootBundle.load('images/marker.png'); + return data.buffer.asUint8List(); + } + // Called when the map is created - void _onMapCreated(MapboxMapController controller) { + void _onMapCreated(MapboxMapController controller) async { mapController = controller; // Log the map controller and coordinates @@ -103,16 +111,28 @@ class _MapboxpagesState extends State with ShowErrorDialog { // 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 - ); + // Ensure the coordinates are valid + if (latitude != 0.0 && longitude != 0.0) { + // Load marker image as Uint8List + final markerImage = await _loadMarkerImage(); - // Debugging symbol options - print("Adding symbol with options: $symbolOptions"); + // Register the image with Mapbox + await mapController!.addImage("custom-marker", markerImage); - mapController.addSymbol(symbolOptions); + final symbolOptions = SymbolOptions( + geometry: LatLng(latitude, longitude), + iconImage: "custom-marker", // Use the registered custom marker + iconSize: 0.5, // Optional: Adjust size + ); + + // Debugging symbol options + print("Adding symbol with options: $symbolOptions"); + + // Add symbol to map + mapController!.addSymbol(symbolOptions); + } else { + print("Error: Invalid coordinates, cannot add symbol."); + } } catch (e) { // Handle any exception that occurs when adding the symbol print("Error when adding symbol: $e"); diff --git a/covas_mobile/pubspec.lock b/covas_mobile/pubspec.lock index 2f313c7..8440d75 100644 --- a/covas_mobile/pubspec.lock +++ b/covas_mobile/pubspec.lock @@ -165,10 +165,10 @@ packages: dependency: transitive description: name: file_selector_linux - sha256: "712ce7fab537ba532c8febdb1a8f167b32441e74acd68c3ccb2e36dcb52c4ab2" + sha256: b2b91daf8a68ecfa4a01b778a6f52edef9b14ecd506e771488ea0f2e0784198b url: "https://pub.dev" source: hosted - version: "0.9.3" + version: "0.9.3+1" file_selector_macos: dependency: transitive description: @@ -809,10 +809,10 @@ packages: dependency: transitive description: name: url_launcher_linux - sha256: e2b9622b4007f97f504cd64c0128309dfb978ae66adbe944125ed9e1750f06af + sha256: "4e9ba368772369e3e08f231d2301b4ef72b9ff87c31192ef471b380ef29a4935" url: "https://pub.dev" source: hosted - version: "3.2.0" + version: "3.2.1" url_launcher_macos: dependency: transitive description: