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. @Deprecated("[avoid] is deprecated, use [avoids] instead.") RouteType? avoid,
  7. List<RouteType> avoids = const <RouteType>[],
  8. String? language,
  9. Unit? units,
  10. String? region,
  11. Object? arrivalTime,
  12. Object? departureTime,
  13. List<TransitMode> transitMode = const <TransitMode>[],
  14. TrafficModel? trafficModel,
  15. 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,
  @Deprecated("[avoid] is deprecated, use [avoids] instead.")
      RouteType? avoid,
  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.toApiString();
  }

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

  if (avoid != null) {
    avoids = [
      ...avoids,
      avoid,
    ];
  }

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

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

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