2022-05-18 19:09:26 +05:30
|
|
|
import 'dart:convert';
|
2022-09-22 20:36:56 +05:30
|
|
|
import 'package:get/get.dart';
|
2022-05-18 19:09:26 +05:30
|
|
|
import 'package:http/http.dart' as http;
|
2022-07-09 22:51:34 +05:30
|
|
|
import 'package:rogapp/model/destination.dart';
|
2022-09-22 20:36:56 +05:30
|
|
|
import 'package:rogapp/pages/destination/destination_controller.dart';
|
2022-05-18 19:09:26 +05:30
|
|
|
|
2023-10-06 16:25:21 +05:30
|
|
|
class MatrixService {
|
|
|
|
|
static Future<Map<String, dynamic>> getDestinations(
|
|
|
|
|
List<Destination> destinations) async {
|
|
|
|
|
final DestinationController destinationController =
|
|
|
|
|
Get.find<DestinationController>();
|
2022-09-22 20:36:56 +05:30
|
|
|
|
2022-05-18 19:09:26 +05:30
|
|
|
String locs = "";
|
|
|
|
|
String origin = "";
|
2022-09-29 15:32:33 +05:30
|
|
|
String destination = "";
|
2022-05-18 19:09:26 +05:30
|
|
|
int i = 0;
|
2023-10-06 16:25:21 +05:30
|
|
|
for (Destination d in destinations) {
|
2024-01-18 17:11:36 +05:30
|
|
|
//print("---- getting matrix for $d ------------");
|
2022-07-10 23:50:43 +05:30
|
|
|
|
2023-10-06 16:25:21 +05:30
|
|
|
if (i == 0) {
|
2022-07-09 22:51:34 +05:30
|
|
|
origin = "${d.lat}, ${d.lon}";
|
2022-09-29 15:32:33 +05:30
|
|
|
i++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 16:25:21 +05:30
|
|
|
if (i == (destinations.length - 1)) {
|
2022-09-29 15:32:33 +05:30
|
|
|
destination = "${d.lat}, ${d.lon}";
|
|
|
|
|
i++;
|
|
|
|
|
continue;
|
2022-05-18 19:09:26 +05:30
|
|
|
}
|
|
|
|
|
|
2023-10-06 16:25:21 +05:30
|
|
|
//print("lat is ${d.lat}, long is ${d.lon}");
|
2022-07-09 22:51:34 +05:30
|
|
|
locs += "${d.lat}, ${d.lon}|";
|
2022-05-18 19:09:26 +05:30
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-16 14:53:32 +05:30
|
|
|
locs = "optimize:false|$locs";
|
2022-09-29 15:32:33 +05:30
|
|
|
|
2023-10-06 16:25:21 +05:30
|
|
|
String mode = "walking";
|
2022-09-22 20:36:56 +05:30
|
|
|
switch (destinationController.travelMode.value) {
|
|
|
|
|
case 1:
|
2023-10-06 16:25:21 +05:30
|
|
|
mode = "driving";
|
2022-09-22 20:36:56 +05:30
|
|
|
break;
|
|
|
|
|
case 2:
|
2023-10-06 16:25:21 +05:30
|
|
|
mode = "transit";
|
|
|
|
|
break;
|
2022-09-22 20:36:56 +05:30
|
|
|
default:
|
2023-10-06 16:25:21 +05:30
|
|
|
mode = "walking";
|
2022-09-22 20:36:56 +05:30
|
|
|
break;
|
|
|
|
|
}
|
2022-05-18 19:09:26 +05:30
|
|
|
|
|
|
|
|
Map<String, dynamic> cats = {};
|
2023-10-06 16:25:21 +05:30
|
|
|
String url =
|
2024-03-06 11:40:00 +05:30
|
|
|
"https://maps.googleapis.com/maps/api/directions/json?destination=$destination&mode=$mode&waypoints=$locs&origin=$origin&key=AIzaSyCN2xFsqFyadWwpjiFxymrxzS6G1tNzraI";
|
2024-01-18 17:11:36 +05:30
|
|
|
//print('++++++++$url');
|
2023-10-06 16:25:21 +05:30
|
|
|
final http.Response response =
|
|
|
|
|
await http.get(Uri.parse(url), headers: <String, String>{
|
|
|
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
|
|
|
});
|
2022-05-18 19:09:26 +05:30
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
cats = json.decode(utf8.decode(response.bodyBytes));
|
|
|
|
|
}
|
|
|
|
|
return cats;
|
|
|
|
|
}
|
|
|
|
|
}
|