3/27/2
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:SEFER/constant/table_names.dart';
|
||||
import 'package:SEFER/controller/home/captin/home_captain_controller.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
import 'package:location/location.dart';
|
||||
@@ -19,9 +20,9 @@ class LocationController extends GetxController {
|
||||
bool isLoading = false;
|
||||
late double heading = 0;
|
||||
late double accuracy = 0;
|
||||
LatLng? previousLocation;
|
||||
late double previousTime = 0;
|
||||
late double latitude;
|
||||
late double totalDistance;
|
||||
late double totalDistance = 0;
|
||||
late double longitude;
|
||||
late DateTime time;
|
||||
late double speed = 0;
|
||||
@@ -63,9 +64,10 @@ class LocationController extends GetxController {
|
||||
'longitude': myLocation.longitude.toString(),
|
||||
'heading': heading.toString(),
|
||||
'speed': speed.toString(),
|
||||
'distance': totalDistance == 0 ? '0' : totalDistance.toString(),
|
||||
'status': box.read(BoxName.statusDriverLocation).toString()
|
||||
});
|
||||
if (Get.find<MapDriverController>().rideId == null) {
|
||||
if (Get.find<HomeCaptainController>().rideId == '0') {
|
||||
await sql.insertData({
|
||||
'driver_id': box.read(BoxName.driverID),
|
||||
'latitude': myLocation.latitude.toString(),
|
||||
@@ -133,20 +135,43 @@ class LocationController extends GetxController {
|
||||
(_locationData.latitude != null && _locationData.longitude != null
|
||||
? LatLng(_locationData.latitude!, _locationData.longitude!)
|
||||
: null)!;
|
||||
speed = _locationData.speed!;
|
||||
heading = _locationData.heading!;
|
||||
// Calculate the distance between the current location and the previous location
|
||||
if (previousLocation != null) {
|
||||
// double distance = myLocation.distanceBetween(previousLocation);
|
||||
// totalDistance += distance;
|
||||
}
|
||||
if (Get.find<HomeCaptainController>().rideId == 'rideId') {
|
||||
if (previousTime > 0) {
|
||||
double distance = calculateDistanceInKmPerHour(
|
||||
previousTime, _locationData.time, speed);
|
||||
print('distance $distance');
|
||||
totalDistance += distance;
|
||||
}
|
||||
print('totalDistance: $totalDistance');
|
||||
|
||||
previousLocation = myLocation;
|
||||
previousTime = _locationData.time!;
|
||||
}
|
||||
// Print location details
|
||||
// print('myLocation: ${myLocation}');
|
||||
// print('Accuracy: ${_locationData.accuracy}');
|
||||
// print('Latitude: ${_locationData.latitude}');
|
||||
// print('Longitude: ${_locationData.longitude}');
|
||||
// print('Time: ${_locationData.time}');
|
||||
print('myLocation: ${myLocation}');
|
||||
print('Accuracy: ${_locationData.accuracy}');
|
||||
print('Latitude: ${_locationData.latitude}');
|
||||
print('Longitude: ${_locationData.longitude}');
|
||||
print('Time: ${_locationData.time}');
|
||||
print('speed: ${_locationData.speed}');
|
||||
print('Heading: ${_locationData.heading}');
|
||||
// isLoading = false;
|
||||
update();
|
||||
}
|
||||
|
||||
double calculateDistanceInKmPerHour(
|
||||
double? startTime, double? endTime, double speedInMetersPerSecond) {
|
||||
// Calculate the time difference in hours
|
||||
double timeDifferenceInHours = (endTime! - startTime!) / 1000 / 3600;
|
||||
|
||||
// Convert speed to kilometers per hour
|
||||
double speedInKmPerHour = speedInMetersPerSecond * 3.6;
|
||||
|
||||
// Calculate the distance in kilometers
|
||||
double distanceInKilometers = speedInKmPerHour * timeDifferenceInHours;
|
||||
|
||||
return distanceInKilometers;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user