buildUrl method

String buildUrl({
  1. required Object origin,
  2. required Object destination,
  3. TravelMode? travelMode,
  4. List<Waypoint> waypoints = const <Waypoint>[],
  5. bool alternatives = false,
  6. List<RouteType> avoids = const <RouteType>[],
  7. String? language,
  8. Unit? units,
  9. String? region,
  10. Object? arrivalTime,
  11. Object? departureTime,
  12. List<TransitMode> transitMode = const <TransitMode>[],
  13. TrafficModel? trafficModel,
  14. TransitRoutingPreferences? transitRoutingPreference,
})

Implementation

String buildUrl({
  required Object /*Location|String*/ origin,
  required Object /*Location|String*/ destination,
  TravelMode? travelMode,
  List<Waypoint> waypoints = const <Waypoint>[],
  bool alternatives = false,
  List<RouteType> avoids = const <RouteType>[],
  String? language,
  Unit? units,
  String? region,
  Object? /*DateTime|num*/ arrivalTime,
  Object? /*DateTime|num|String('now')*/ departureTime,
  List<TransitMode> transitMode = const <TransitMode>[],
  TrafficModel? trafficModel,
  TransitRoutingPreferences? transitRoutingPreference,
}) {
  final params = <String, String>{};

  if (origin is! Location && origin is! String) {
    throw ArgumentError("'origin' must be a '$String' or a '$Location'");
  }
  params['origin'] = origin.toString();

  if (destination is! Location && destination is! String) {
    throw ArgumentError("'destination' must be a '$String' or a '$Location'");
  }
  params['destination'] = destination.toString();

  if (departureTime != null) {
    if (departureTime is! DateTime &&
        departureTime is! num &&
        departureTime != 'now') {
      throw ArgumentError(
          "'departureTime' must be a '$num' or a '$DateTime'");
    }

    params['departure_time'] = departureTime is DateTime
        ? (departureTime.millisecondsSinceEpoch ~/ 1000).toString()
        : departureTime.toString();
  }

  if (arrivalTime != null) {
    if (arrivalTime is! DateTime && arrivalTime is! num) {
      throw ArgumentError("'arrivalTime' must be a '$num' or a '$DateTime'");
    }

    params['arrival_time'] = arrivalTime is DateTime
        ? (arrivalTime.millisecondsSinceEpoch ~/ 1000).toString()
        : arrivalTime.toString();
  }

  if (waypoints.isNotEmpty == true) {
    if (alternatives == true) {
      throw ArgumentError(
        "'alternatives' is only available for requests without intermediate waypoints",
      );
    }

    params['waypoints'] = waypoints.join('|');
  }

  if (travelMode != null) {
    params['mode'] = travelMode.name;
  }

  if (alternatives) {
    params['alternatives'] = alternatives.toString();
  }

  if (avoids.isNotEmpty) {
    params['avoid'] = avoids.map((t) => t.name).join('|');
  }

  if (language != null) {
    params['language'] = language;
  }

  if (units != null) {
    params['units'] = units.name;
  }

  if (region != null) {
    params['region'] = region;
  }

  if (trafficModel != null) {
    params['traffic_model'] = trafficModel.name;
  }

  if (transitMode.isNotEmpty) {
    params['transit_mode'] = transitMode.map((t) => t.name).join('|');
  }

  if (transitRoutingPreference != null) {
    params['transit_routing_preference'] = transitRoutingPreference.name;
  }

  if (apiKey != null) {
    params['key'] = apiKey!;
  }

  return url.replace(queryParameters: params).toString();
}