add marker

This commit is contained in:
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: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<Mapboxpages> 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<Mapboxpages> 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<Mapboxpages> 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<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
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<Mapboxpages> 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");