buildUrl method

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

Implementation

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

  if (origin is! List<Location> && origin is! List<String>) {
    throw ArgumentError("'origin' must be a '$String' or a '$Location'");
  }
  params['origins'] = origin.join('|');

  if (destination is! List<Location> && destination is! List<String>) {
    throw ArgumentError("'destination' must be a '$String' or a '$Location'");
  }
  params['destinations'] = destination.join('|');

  if (destination is! List<Location> && destination is! List<String>) {
    throw ArgumentError("'destination' must be a '$String' or a '$Location'");
  }
  if (departureTime != null &&
      departureTime is! DateTime &&
      departureTime is! num &&
      departureTime != 'now') {
    throw ArgumentError("'departureTime' must be a '$num' or a '$DateTime'");
  }
  if (arrivalTime != null &&
      arrivalTime is! DateTime &&
      arrivalTime is! num) {
    throw ArgumentError("'arrivalTime' must be a '$num' or a '$DateTime'");
  }

  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 (travelMode != null) {
    params['mode'] = travelMode.toApiString();
  }

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

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

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

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

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

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

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

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

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

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