getDirections method

Future<Map<String, dynamic>> getDirections(
  1. String origin,
  2. String destination, {
  3. Client? httpClient,
})

Implementation

Future<Map<String, dynamic>> getDirections(String origin, String destination,
    {http.Client? httpClient}) async {
  final String url =
      'https://maps.googleapis.com/maps/api/directions/json?origin=$origin&destination=$destination&key=$key';

  var response = await http.get(Uri.parse(url));

  if (response.statusCode == 200) {
    var json = convert.jsonDecode(response.body);

    var results = {
      'bounds_ne': json['routes'][0]['bounds']['northeast'],
      'bounds_sw': json['routes'][0]['bounds']['southwest'],
      'start_location': json['routes'][0]['legs'][0]['start_location'],
      'end_location': json['routes'][0]['legs'][0]['end_location'],
      'polyline': json['routes'][0]['overview_polyline']['points'],
      'polyline_decoded': PolylinePoints()
          .decodePolyline(json['routes'][0]['overview_polyline']['points']),
    };

    // print(results);
    return results;
  } else {
    // Return null or handle error as needed
    return {};
  }
}