add marker
This commit is contained in:
parent
2e1b25264a
commit
be8b0d3b66
@ -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
|
||||
// Ensure the coordinates are valid
|
||||
if (latitude != 0.0 && longitude != 0.0) {
|
||||
// Load marker image as Uint8List
|
||||
final markerImage = await _loadMarkerImage();
|
||||
|
||||
// Register the image with Mapbox
|
||||
await mapController!.addImage("custom-marker", markerImage);
|
||||
|
||||
final symbolOptions = SymbolOptions(
|
||||
geometry: LatLng(latitude, longitude),
|
||||
iconImage: "marker-15", // Make sure this icon exists in your assets
|
||||
iconImage: "custom-marker", // Use the registered custom marker
|
||||
iconSize: 0.5, // Optional: Adjust size
|
||||
);
|
||||
|
||||
// Debugging symbol options
|
||||
print("Adding symbol with options: $symbolOptions");
|
||||
|
||||
mapController.addSymbol(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");
|
||||
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user