calculateRoute method

Future<CalculateRouteResponse> calculateRoute({
  1. required String calculatorName,
  2. required List<double> departurePosition,
  3. required List<double> destinationPosition,
  4. DateTime? arrivalTime,
  5. CalculateRouteCarModeOptions? carModeOptions,
  6. bool? departNow,
  7. DateTime? departureTime,
  8. DistanceUnit? distanceUnit,
  9. bool? includeLegGeometry,
  10. String? key,
  11. OptimizationMode? optimizeFor,
  12. TravelMode? travelMode,
  13. CalculateRouteTruckModeOptions? truckModeOptions,
  14. List<List<double>>? waypointPositions,
})
Calculates a route given the following required parameters: DeparturePosition and DestinationPosition. Requires that you first create a route calculator resource.

By default, a request that doesn't specify a departure time uses the best time of day to travel with the best traffic conditions when calculating the route.

Additional options include:

  • Specifying a departure time using either DepartureTime or DepartNow. This calculates a route based on predictive traffic data at the given time.
  • Specifying a travel mode using TravelMode sets the transportation mode used to calculate the routes. This also lets you specify additional route preferences in CarModeOptions if traveling by Car, or TruckModeOptions if traveling by Truck.

May throw AccessDeniedException. May throw InternalServerException. May throw ResourceNotFoundException. May throw ThrottlingException. May throw ValidationException.

Parameter calculatorName : The name of the route calculator resource that you want to use to calculate the route.

Parameter departurePosition : The start position for the route. Defined in World Geodetic System (WGS 84) format: [longitude, latitude].

  • For example, \[-123.115, 49.285\]
Valid Values: \[-180 to 180,-90 to 90\]

Parameter destinationPosition : The finish position for the route. Defined in World Geodetic System (WGS 84) format: [longitude, latitude].

  • For example, \[-122.339, 47.615\]
Valid Values: \[-180 to 180,-90 to 90\]

Parameter arrivalTime : Specifies the desired time of arrival. Uses the given time to calculate the route. Otherwise, the best time of day to travel with the best traffic conditions is used to calculate the route.

Parameter carModeOptions : Specifies route preferences when traveling by Car, such as avoiding routes that use ferries or tolls.

Requirements: TravelMode must be specified as Car.

Parameter departNow : Sets the time of departure as the current time. Uses the current time to calculate a route. Otherwise, the best time of day to travel with the best traffic conditions is used to calculate the route.

Default Value: false

Valid Values: false | true

Parameter departureTime : Specifies the desired time of departure. Uses the given time to calculate the route. Otherwise, the best time of day to travel with the best traffic conditions is used to calculate the route.

  • In ISO 8601 format: YYYY-MM-DDThh:mm:ss.sssZ. For example, 2020–07-2T12:15:20.000Z+01:00

Parameter distanceUnit : Set the unit system to specify the distance.

Default Value: Kilometers

Parameter includeLegGeometry : Set to include the geometry details in the result for each path between a pair of positions.

Default Value: false

Valid Values: false | true

Parameter key : The optional API key to authorize the request.

Parameter optimizeFor : Specifies the distance to optimize for when calculating a route.

Parameter travelMode : Specifies the mode of transport when calculating a route. Used in estimating the speed of travel and road compatibility. You can choose Car, Truck, Walking, Bicycle or Motorcycle as options for the TravelMode.

Truck is not available for Grab.

For more details on the using Grab for routing, including areas of coverage, see GrabMaps in the Amazon Location Service Developer Guide. The TravelMode you specify also determines how you specify route preferences:

  • If traveling by Car use the CarModeOptions parameter.
  • If traveling by Truck use the TruckModeOptions parameter.
Default Value: Car

Parameter truckModeOptions : Specifies route preferences when traveling by Truck, such as avoiding routes that use ferries or tolls, and truck specifications to consider when choosing an optimal road.

Requirements: TravelMode must be specified as Truck.

Parameter waypointPositions : Specifies an ordered list of up to 23 intermediate positions to include along a route between the departure position and destination position.

  • For example, from the DeparturePosition \[-123.115, 49.285\], the route follows the order that the waypoint positions are given \[\[-122.757, 49.0021\],\[-122.349, 47.620\]\]

Implementation

Future<CalculateRouteResponse> calculateRoute({
  required String calculatorName,
  required List<double> departurePosition,
  required List<double> destinationPosition,
  DateTime? arrivalTime,
  CalculateRouteCarModeOptions? carModeOptions,
  bool? departNow,
  DateTime? departureTime,
  DistanceUnit? distanceUnit,
  bool? includeLegGeometry,
  String? key,
  OptimizationMode? optimizeFor,
  TravelMode? travelMode,
  CalculateRouteTruckModeOptions? truckModeOptions,
  List<List<double>>? waypointPositions,
}) async {
  final $query = <String, List<String>>{
    if (key != null) 'key': [key],
  };
  final $payload = <String, dynamic>{
    'DeparturePosition': departurePosition.map(_s.encodeJsonDouble).toList(),
    'DestinationPosition':
        destinationPosition.map(_s.encodeJsonDouble).toList(),
    if (arrivalTime != null) 'ArrivalTime': iso8601ToJson(arrivalTime),
    if (carModeOptions != null) 'CarModeOptions': carModeOptions,
    if (departNow != null) 'DepartNow': departNow,
    if (departureTime != null) 'DepartureTime': iso8601ToJson(departureTime),
    if (distanceUnit != null) 'DistanceUnit': distanceUnit.value,
    if (includeLegGeometry != null) 'IncludeLegGeometry': includeLegGeometry,
    if (optimizeFor != null) 'OptimizeFor': optimizeFor.value,
    if (travelMode != null) 'TravelMode': travelMode.value,
    if (truckModeOptions != null) 'TruckModeOptions': truckModeOptions,
    if (waypointPositions != null)
      'WaypointPositions': waypointPositions
          .map((e) => e.map(_s.encodeJsonDouble).toList())
          .toList(),
  };
  final response = await _protocol.send(
    payload: $payload,
    method: 'POST',
    requestUri:
        '/routes/v0/calculators/${Uri.encodeComponent(calculatorName)}/calculate/route',
    queryParams: $query,
    hostPrefix: 'routes.',
    exceptionFnMap: _exceptionFns,
  );
  return CalculateRouteResponse.fromJson(response);
}