Files
rog_app/lib/services/maxtrix_service.dart

74 lines
1.8 KiB
Dart
Raw Normal View History

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
class MatrixService{
2022-07-09 22:51:34 +05:30
static Future<Map<String, dynamic>> getDestinations(List<Destination> destinations) async {
2022-05-18 19:09:26 +05:30
2022-09-22 20:36:56 +05:30
final DestinationController destinationController = Get.find<DestinationController>();
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;
2022-07-09 22:51:34 +05:30
for(Destination d in destinations){
2022-05-18 19:09:26 +05:30
2022-07-10 23:50:43 +05:30
print("---- getting matrix for ${d} ------------");
2022-05-18 19:09:26 +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;
}
if(i == (destinations.length - 1)){
destination = "${d.lat}, ${d.lon}";
i++;
continue;
2022-05-18 19:09:26 +05:30
}
2022-07-09 22:51:34 +05:30
print("lat is ${d.lat}, long is ${d.lon}");
locs += "${d.lat}, ${d.lon}|";
2022-05-18 19:09:26 +05:30
i++;
}
2022-09-29 15:32:33 +05:30
locs = "optimize:false|${locs}";
2022-09-22 20:36:56 +05:30
String _mode = "walking";
switch (destinationController.travelMode.value) {
case 1:
_mode = "driving";
break;
case 2:
_mode = "transit";
break;
default:
_mode = "walking";
break;
}
2022-05-18 19:09:26 +05:30
Map<String, dynamic> cats = {};
2022-09-29 15:32:33 +05:30
String url = "https://maps.googleapis.com/maps/api/directions/json?destination=${destination}&mode=${_mode}&waypoints=${locs}&origin=${origin}&key=AIzaSyAUBI1ablMKuJwGj2-kSuEhvYxvB1A-mOE";
2023-06-02 10:50:56 +05:30
print('@@@@@@+++ matrix_service GETDESTINATIONS - GET, @@@@@@ ${url}');
2022-05-18 19:09:26 +05:30
final http.Response response = await http.get(
Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
}
);
if (response.statusCode == 200) {
cats = json.decode(utf8.decode(response.bodyBytes));
}
return cats;
}
}