add marker

This commit is contained in:
Valentin CZERYBA 2024-11-13 23:47:40 +01:00
parent 2e1b25264a
commit be8b0d3b66
2 changed files with 37 additions and 17 deletions

View File

@ -1,6 +1,7 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:typed_data';
import 'dart:io'; import 'dart:io';
import 'package:flutter/services.dart'; // For loading assets
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:mapbox_gl/mapbox_gl.dart'; import 'package:mapbox_gl/mapbox_gl.dart';
@ -41,7 +42,7 @@ class Mapboxpages extends StatefulWidget {
class _MapboxpagesState extends State<Mapboxpages> with ShowErrorDialog { class _MapboxpagesState extends State<Mapboxpages> with ShowErrorDialog {
late String mapboxAccessToken; late String mapboxAccessToken;
late MapboxMapController mapController; MapboxMapController? mapController; // Mark mapController as nullable
double longitude = 0.0; double longitude = 0.0;
double latitude = 0.0; double latitude = 0.0;
bool isLoading = true; bool isLoading = true;
@ -62,8 +63,6 @@ class _MapboxpagesState extends State<Mapboxpages> with ShowErrorDialog {
// Fetch event location using the title (address or name) // Fetch event location using the title (address or name)
await _fetchEventLocation(); await _fetchEventLocation();
// Set the state after fetching coordinates
} }
// Fetch location coordinates using the event title // Fetch location coordinates using the event title
@ -80,11 +79,14 @@ class _MapboxpagesState extends State<Mapboxpages> with ShowErrorDialog {
if (geocodeData['features'].isNotEmpty) { if (geocodeData['features'].isNotEmpty) {
final coordinates = final coordinates =
geocodeData['features'][0]['geometry']['coordinates']; geocodeData['features'][0]['geometry']['coordinates'];
print("geodate : ${geocodeData['features'][0]}");
longitude = coordinates[0]; // Longitude longitude = coordinates[0]; // Longitude
latitude = coordinates[1]; // Latitude latitude = coordinates[1]; // Latitude
setState(() { setState(() {
isLoading = false; isLoading = false;
}); });
} else {
showErrorDialog(context, "Location not found.");
} }
} else { } else {
showErrorDialog(context, "Failed to fetch location data."); showErrorDialog(context, "Failed to fetch location data.");
@ -92,8 +94,14 @@ class _MapboxpagesState extends State<Mapboxpages> with ShowErrorDialog {
} }
} }
// Load image from assets as Uint8List
Future<Uint8List> _loadMarkerImage() async {
final ByteData data = await rootBundle.load('images/marker.png');
return data.buffer.asUint8List();
}
// Called when the map is created // Called when the map is created
void _onMapCreated(MapboxMapController controller) { void _onMapCreated(MapboxMapController controller) async {
mapController = controller; mapController = controller;
// Log the map controller and coordinates // Log the map controller and coordinates
@ -103,16 +111,28 @@ class _MapboxpagesState extends State<Mapboxpages> with ShowErrorDialog {
// Check if the mapController is really initialized // Check if the mapController is really initialized
if (mapController != null) { if (mapController != null) {
try { try {
// Check if symbol options are correctly set // Ensure the coordinates are valid
final symbolOptions = SymbolOptions( if (latitude != 0.0 && longitude != 0.0) {
geometry: LatLng(latitude, longitude), // Load marker image as Uint8List
iconImage: "marker-15", // Make sure this icon exists in your assets final markerImage = await _loadMarkerImage();
);
// Debugging symbol options // Register the image with Mapbox
print("Adding symbol with options: $symbolOptions"); 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) { } catch (e) {
// Handle any exception that occurs when adding the symbol // Handle any exception that occurs when adding the symbol
print("Error when adding symbol: $e"); print("Error when adding symbol: $e");

View File

@ -165,10 +165,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: file_selector_linux name: file_selector_linux
sha256: "712ce7fab537ba532c8febdb1a8f167b32441e74acd68c3ccb2e36dcb52c4ab2" sha256: b2b91daf8a68ecfa4a01b778a6f52edef9b14ecd506e771488ea0f2e0784198b
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.9.3" version: "0.9.3+1"
file_selector_macos: file_selector_macos:
dependency: transitive dependency: transitive
description: description:
@ -809,10 +809,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: url_launcher_linux name: url_launcher_linux
sha256: e2b9622b4007f97f504cd64c0128309dfb978ae66adbe944125ed9e1750f06af sha256: "4e9ba368772369e3e08f231d2301b4ef72b9ff87c31192ef471b380ef29a4935"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "3.2.0" version: "3.2.1"
url_launcher_macos: url_launcher_macos:
dependency: transitive dependency: transitive
description: description: