flutter_google_track_and_trace/lib/src/directions_repository.dart

108 lines
3 KiB
Dart
Raw Normal View History

part of flutter_google_track_and_trace;
2021-09-28 08:18:14 +02:00
2021-09-28 10:37:48 +02:00
enum TravelMode { driving, walking, bicycling, transit }
2021-09-28 08:18:14 +02:00
class DirectionsRepository {
static const String _baseUrl = '/maps/api/directions/json';
/// get the route between the two coordinates
Future<Directions?> getDirections({
2021-09-28 08:18:14 +02:00
required LatLng origin,
required LatLng destination,
required TravelMode mode,
required String key,
}) async {
try {
2021-09-30 10:09:57 +02:00
var queryParameters = {
2021-09-28 08:18:14 +02:00
'origin': '${origin.latitude},${origin.longitude}',
'destination': '${destination.latitude},${destination.longitude}',
2021-09-30 10:09:57 +02:00
'key': key, // get this key from the controller
2021-09-28 08:18:14 +02:00
'mode': <TravelMode, String>{
2021-09-28 10:37:48 +02:00
TravelMode.driving: 'driving',
TravelMode.bicycling: 'bicycling',
TravelMode.transit: 'transit',
TravelMode.walking: 'walking',
2021-09-28 08:18:14 +02:00
}[mode],
};
2021-09-30 10:09:57 +02:00
var uri = Uri.https('maps.googleapis.com', _baseUrl, queryParameters);
var response = await http.get(
uri,
headers: {
HttpHeaders.contentTypeHeader: 'application/json',
},
);
2021-09-28 08:18:14 +02:00
if (response.statusCode == 200) {
try {
return Directions.fromMap(jsonDecode(response.body));
} on GoogleMapsException catch (_) {
return null;
}
2021-09-28 08:18:14 +02:00
}
} on HttpException catch (e) {
2021-09-30 10:09:57 +02:00
debugPrint(e.message);
2021-09-28 08:18:14 +02:00
}
2021-09-30 10:09:57 +02:00
throw GoogleMapsException(
2022-02-08 13:29:11 +01:00
'Unable to retrieve directions from Google Maps API',
);
2021-09-28 08:18:14 +02:00
}
}
class Directions {
const Directions({
required this.bounds,
required this.polylinePoints,
required this.totalDistance,
required this.totalDuration,
});
2021-09-29 12:18:53 +02:00
/// map the json response to a [Directions] object
2021-09-28 08:18:14 +02:00
factory Directions.fromMap(Map<String, dynamic> map) {
if ((map['routes'] as List).isEmpty) {
throw GoogleMapsException('No Routes available');
}
2021-09-30 10:09:57 +02:00
var data = Map<String, dynamic>.from((map['routes'] as List)[0]);
2021-09-28 08:18:14 +02:00
2021-09-30 10:09:57 +02:00
var northeast = data['bounds']['northeast'];
var southwest = data['bounds']['southwest'];
var bounds = LatLngBounds(
2021-09-28 08:18:14 +02:00
southwest: LatLng(southwest['lat'], southwest['lng']),
northeast: LatLng(northeast['lat'], northeast['lng']),
);
2021-09-30 10:09:57 +02:00
var distance = 0;
var duration = 0;
2021-09-28 08:18:14 +02:00
if ((data['legs'] as List).isNotEmpty) {
2021-09-30 10:09:57 +02:00
var leg = (data['legs'] as List)[0];
2021-09-29 10:07:56 +02:00
distance = leg['distance']['value'];
duration = leg['duration']['value'];
2021-09-28 08:18:14 +02:00
}
return Directions(
bounds: bounds,
polylinePoints:
PolylinePoints().decodePolyline(data['overview_polyline']['points']),
totalDistance: distance,
totalDuration: duration,
);
}
2021-09-30 10:09:57 +02:00
final LatLngBounds bounds;
final List<PointLatLng> polylinePoints;
final int totalDistance;
final int totalDuration;
2021-09-28 08:18:14 +02:00
}
class GoogleMapsException implements Exception {
GoogleMapsException(this.message);
/// The unhandled [error] object.
final String message;
@override
String toString() {
2021-09-29 12:18:53 +02:00
return 'Error occurred in Track&Trace package:\n'
2021-09-28 08:18:14 +02:00
'$message';
}
}