This commit is contained in:
Hamza-Ayed
2023-09-03 13:48:55 +03:00
parent 4628f91d3e
commit ef6d3604cd
12 changed files with 341 additions and 205 deletions

View File

@@ -2,7 +2,6 @@ import 'dart:async';
import 'dart:convert';
import 'dart:math' show sqrt, pi, cos, sin;
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_polyline_algorithm/google_polyline_algorithm.dart';
@@ -10,6 +9,7 @@ import 'package:location/location.dart';
import 'package:ride/constant/box_name.dart';
import 'package:ride/constant/credential.dart';
import 'package:ride/constant/links.dart';
import 'package:ride/controller/firebase/firbase_messge.dart';
import 'package:ride/controller/functions/crud.dart';
import 'package:ride/controller/functions/secure_storage.dart';
import 'package:ride/views/widgets/elevated_btn.dart';
@@ -115,11 +115,48 @@ class MapController extends GetxController {
update();
}
changeConfirmRide() {
changeConfirmRide() async {
rideConfirm = true;
print('rideConfirm= $rideConfirm');
//TODO add ride and get ride id and details
await CRUD().post(link: AppLink.addRides, payload: {
"start_location":
'${data[0]["start_location"]['lat']},${data[0]["start_location"]['lng']}',
"end_location":
'${data[0]["end_location"]['lat']},${data[0]["end_location"]['lng']}',
"date": DateTime.now().toString(),
"time": DateTime.now().toString(),
"endtime": durationToAdd.toString(),
"price": totalPassenger.toString(),
"passenger_id": box.read(BoxName.pasengerID).toString(),
"driver_id": dataCarsLocationByPassenger['message'][0]['id'].toString(),
"status": "active",
"price_for_driver": totalDriver.toString(),
"price_for_passenger": totaME.toString(),
"distance": distance.toString()
}).then((value) {
print(value);
List<String> body = [
'${data[0]["start_location"]['lat']},${data[0]["start_location"]['lng']}',
'${data[0]["end_location"]['lat']},${data[0]["end_location"]['lng']}',
totalPassenger.toString(),
totalDriver.toString(),
duration.toString(),
distance.toString(),
];
FirebasMessagesController().sendNotificationDriverId(
'Order',
'body',
body[0],
body[1],
body[2],
body[3],
body[4],
body[5],
box.read(BoxName.tokenFCM).toString(), //TODO change to Driver
);
});
update();
}
@@ -369,7 +406,8 @@ class MapController extends GetxController {
}
void reloadMarkers() async {
await getCarsLocationByPassenger();
//TODO await getCarsLocationByPassenger();
// Clear existing markers
markers.clear();
getNearestDriverByPassengerLocation();
@@ -392,59 +430,62 @@ class MapController extends GetxController {
String duratioByPassenger = '';
void getNearestDriverByPassengerLocation() async {
double nearestDistance = double.infinity;
for (var i = 0; i < dataCarsLocationByPassenger['message'].length; i++) {
var carLocation = dataCarsLocationByPassenger['message'][i];
if (polylines.isEmpty) {
double nearestDistance = double.infinity;
for (var i = 0; i < dataCarsLocationByPassenger['message'].length; i++) {
var carLocation = dataCarsLocationByPassenger['message'][i];
// double distance1 = Geolocator.distanceBetween(
// mylocation.latitude,
// mylocation.longitude,
// double.parse(carLocation['latitude']),
// double.parse(carLocation['longitude']),
// );
// if (distance1 < nearestDistance) {
// nearestDistance = distance1;
// // nearestCarLocation = carLocation;
// nearestCar = CarLocation(
// distance: distance1,
// id: carLocation['driver_id'],
// latitude: double.parse(carLocation['latitude']),
// longitude: double.parse(carLocation['longitude']),
// );
// }
isloading = true;
update();
// Make API request to get exact distance and duration
String apiUrl =
'${AppLink.googleMapsLink}distancematrix/json?destinations=${carLocation['latitude']},${carLocation['longitude']}&origins=${mylocation.latitude},${mylocation.longitude}&units=metric&key=${AppCredintials.mapAPIKEY}';
var response = await CRUD().getGoogleApi(link: apiUrl, payload: {});
if (response['status'] == "OK") {
var data = response;
// Extract distance and duration from the response and handle accordingly
int distance1 = data['rows'][0]['elements'][0]['distance']['value'];
duration1 = data['rows'][0]['elements'][0]['duration']['value'];
duratioByPassenger = data['rows'][0]['elements'][0]['duration']['text'];
// double distance1 = Geolocator.distanceBetween(
// mylocation.latitude,
// mylocation.longitude,
// double.parse(carLocation['latitude']),
// double.parse(carLocation['longitude']),
// );
// if (distance1 < nearestDistance) {
// nearestDistance = distance1;
// // nearestCarLocation = carLocation;
// nearestCar = CarLocation(
// distance: distance1,
// id: carLocation['driver_id'],
// latitude: double.parse(carLocation['latitude']),
// longitude: double.parse(carLocation['longitude']),
// );
// }
isloading = true;
update();
// Make API request to get exact distance and duration
String apiUrl =
'${AppLink.googleMapsLink}distancematrix/json?destinations=${carLocation['latitude']},${carLocation['longitude']}&origins=${mylocation.latitude},${mylocation.longitude}&units=metric&key=${AppCredintials.mapAPIKEY}';
var response = await CRUD().getGoogleApi(link: apiUrl, payload: {});
if (response['status'] == "OK") {
var data = response;
// Extract distance and duration from the response and handle accordingly
int distance1 = data['rows'][0]['elements'][0]['distance']['value'];
duration1 = data['rows'][0]['elements'][0]['duration']['value'];
duratioByPassenger =
data['rows'][0]['elements'][0]['duration']['text'];
if (distance1 < nearestDistance) {
nearestDistance = distance1.toDouble();
if (distance1 < nearestDistance) {
nearestDistance = distance1.toDouble();
nearestCar = CarLocation(
distance: distance1.toDouble(),
duration: duration1.toDouble(),
id: carLocation['driver_id'],
latitude: double.parse(carLocation['latitude']),
longitude: double.parse(carLocation['longitude']),
);
isloading = false;
update();
nearestCar = CarLocation(
distance: distance1.toDouble(),
duration: duration1.toDouble(),
id: carLocation['driver_id'],
latitude: double.parse(carLocation['latitude']),
longitude: double.parse(carLocation['longitude']),
);
isloading = false;
update();
}
}
}
// Handle the distance and duration as needed
else {
print(
'Failed to retrieve distance and duration: ${response['status']}');
// Handle the failure case
// Handle the distance and duration as needed
else {
print(
'Failed to retrieve distance and duration: ${response['status']}');
// Handle the failure case
}
}
}
print(nearestCar!.distance);
@@ -534,6 +575,7 @@ class MapController extends GetxController {
bool promoTaken = false;
void applyPromoCodetoPassenger() async {
//TAWJIHI
CRUD().get(link: AppLink.getPassengersPromo, payload: {
'promo_code': promo.text,
}).then((value) {
@@ -736,8 +778,8 @@ class MapController extends GetxController {
@override
void onInit() async {
await getLocation();
await getCarsLocationByPassenger();
getNearestDriverByPassengerLocation();
// await getCarsLocationByPassenger();
// getNearestDriverByPassengerLocation();
addCustomPicker();
addCustomCarIcon();
startMarkerReloading();