127 lines
3.5 KiB
Dart
127 lines
3.5 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:Intaleq/constant/links.dart';
|
|
import 'package:Intaleq/controller/functions/crud.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:maplibre_gl/maplibre_gl.dart';
|
|
|
|
class TripMonitorController extends GetxController {
|
|
bool isLoading = false;
|
|
Map tripData = {};
|
|
late String rideId;
|
|
late String driverId;
|
|
MapLibreMapController? mapController;
|
|
List myListString = [];
|
|
late Timer timer;
|
|
late LatLng parentLocation;
|
|
String carIcon = 'car';
|
|
String motoIcon = 'moto';
|
|
String ladyIcon = 'lady';
|
|
double rotation = 0;
|
|
double speed = 0;
|
|
bool isStyleLoaded = false;
|
|
|
|
getLocationParent() async {
|
|
var res = await CRUD().get(
|
|
link: AppLink.getLocationParents, payload: {"driver_id": driverId});
|
|
if (res != 'failure') {
|
|
tripData = jsonDecode(res);
|
|
parentLocation = LatLng(
|
|
double.parse(tripData['message'][0]['latitude'].toString()),
|
|
double.parse(tripData['message'][0]['longitude'].toString()));
|
|
rotation = double.parse(tripData['message'][0]['heading'].toString());
|
|
speed = double.parse(tripData['message'][0]['speed'].toString());
|
|
update();
|
|
}
|
|
}
|
|
|
|
void onMapCreated(MapLibreMapController controller) async {
|
|
mapController = controller;
|
|
update();
|
|
}
|
|
|
|
void onStyleLoaded() async {
|
|
isStyleLoaded = true;
|
|
await _loadMapIcons();
|
|
mapController?.animateCamera(
|
|
CameraUpdate.newLatLng(parentLocation),
|
|
);
|
|
refreshMapElements();
|
|
|
|
// Set up a timer or interval to trigger the marker update every 10 seconds.
|
|
timer = Timer.periodic(const Duration(seconds: 10), (_) async {
|
|
await getLocationParent();
|
|
mapController?.animateCamera(CameraUpdate.newLatLng(parentLocation));
|
|
refreshMapElements();
|
|
update();
|
|
});
|
|
}
|
|
|
|
Future<void> _loadMapIcons() async {
|
|
if (mapController == null) return;
|
|
final icons = {
|
|
'car': 'assets/images/car.png',
|
|
'moto': 'assets/images/moto1.png',
|
|
'lady': 'assets/images/lady1.png',
|
|
};
|
|
for (var entry in icons.entries) {
|
|
final bytes = await rootBundle.load(entry.value);
|
|
await mapController!.addImage(entry.key, bytes.buffer.asUint8List());
|
|
}
|
|
}
|
|
|
|
void refreshMapElements() async {
|
|
if (!isStyleLoaded || mapController == null) return;
|
|
await mapController!.clearSymbols();
|
|
|
|
String iconToUse = carIcon;
|
|
if (tripData['message'] != null && tripData['message'].isNotEmpty) {
|
|
final model = tripData['message'][0]['model'].toString();
|
|
final gender = tripData['message'][0]['gender'].toString();
|
|
if (model.contains('دراجة')) {
|
|
iconToUse = motoIcon;
|
|
} else if (gender == 'Female') {
|
|
iconToUse = ladyIcon;
|
|
}
|
|
}
|
|
|
|
await mapController!.addSymbol(SymbolOptions(
|
|
geometry: parentLocation,
|
|
iconImage: iconToUse,
|
|
iconRotate: rotation,
|
|
textField: 'driver',
|
|
textOpacity: 0,
|
|
));
|
|
}
|
|
|
|
// init() async {
|
|
// final arguments = Get.arguments;
|
|
// driverId = arguments['driverId'];
|
|
// rideId = arguments['rideId'];
|
|
// await getLocationParent();
|
|
// }
|
|
|
|
Future<void> init({String? rideId, String? driverId}) async {
|
|
this.driverId = driverId!;
|
|
this.rideId = rideId!;
|
|
await getLocationParent();
|
|
update();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
timer.cancel();
|
|
mapController = null;
|
|
|
|
super.onClose();
|
|
}
|
|
}
|