本文档介绍了如何创建合乘行程、设置正确的字段,以及如何将行程分配给车辆以完成行程。本教程假定您已设置 Fleet Engine,已创建车辆,拥有可正常运行的司机应用,并且可以选择性地拥有消费者应用。您还应熟悉各种可用于按需行程的行程场景。如需了解相关信息,请参阅以下相关指南:
- 设置 Fleet Engine
- 创建车辆
- 按需行程概览中的行程场景
行程创建基础知识
本部分介绍了在 Fleet Engine 中创建行程所需的请求详细信息。您可以使用 gRPC 和 REST 发出创建请求。
行程字段
使用以下字段在 Fleet Engine 中创建行程。您可以针对不同类型的行程使用不同的字段:单目的地或多目的地行程、连续行程或共享合乘行程。您可以在创建行程时提供可选字段,也可以在稍后更新行程时设置这些字段。
| 名称 | 必需? | 说明 | 
|---|---|---|
| 父级 | 是 | 包含项目 ID 的字符串。此 ID 必须与整个 Fleet Engine 集成中使用的 ID 相同,并且具有相同的服务账号角色。 | 
| trip_id | 是 | 您创建的用于唯一标识相应行程的字符串。行程 ID 有一些限制,如参考文档中所述。 | 
| trip_type | 是 | 将 TripType 设置为以下值,以表示您要创建的行程类型: 
 | 
| pickup_point | 是 | 行程的出发地。 | 
| 中途目的地 | 是 | 仅限多目的地行程:司机在上车地点和下车地点之间访问的中间目的地列表。与  | 
| vehicle_waypoints | 是 | 仅限合乘行程:此字段支持交错来自多个行程的途经点。
    它包含分配车辆的所有剩余途经点,以及本次行程的上车和下车途经点。您可以通过调用  | 
| number_of_passengers | 否 | 行程中的乘客人数。 | 
| dropoff_point | 否 | 行程的目的地。 | 
| vehicle_id | 否 | 分配给行程的车辆的 ID。 | 
示例:创建共享合乘行程
以下后端集成示例演示了如何创建行程并将其分配给车辆作为合乘行程。
// Vehicle with VEHICLE_ID ID is already created and it is assigned Trip A.
static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String TRIP_ID = "shared-trip-A";
static final String VEHICLE_ID = "your-vehicle-id";
static final String TRIP_A_ID = "trip-a-id";
static final String TRIP_B_ID = "trip-b-id";
TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);
String parent = "providers/" + PROJECT_ID;
LatLng tripBPickup =
    LatLng.newBuilder().setLatitude(-12.12314).setLongitude(88.142123).build();
LatLng tripBDropoff =
    LatLng.newBuilder().setLatitude(-14.12314).setLongitude(90.142123).build();
TerminalLocation tripBPickupTerminalLocation =
    TerminalLocation.newBuilder().setPoint(tripBPickup).build();
TerminalLocation tripBDropoffTerminalLocation =
    TerminalLocation.newBuilder().setPoint(tripBDropoff).build();
// TripA already exists and it's assigned to a vehicle with VEHICLE_ID ID.
Trip tripB = Trip.newBuilder()
    .setTripType(TripType.SHARED)
    .setVehicleId(VEHICLE_ID)
    .setPickupPoint(tripBPickupTerminalLocation)
    .setDropoffPoint(tripBDropoffTerminalLocation)
    .addAllVehicleWaypoints(
        // This is where you define the arrival order for unvisited waypoints.
        // If you don't specify an order, then the Fleet Engine adds Trip B's
        // waypoints to the end of Trip A's.
        ImmutableList.of(
            // Trip B's pickup point.
            TripWaypoint.newBuilder()
                .setLocation(tripBPickupTerminalLocation)
                .setTripId(TRIP_B_ID)
                .setWaypointType(WaypointType.PICKUP_WAYPOINT_TYPE)
                .build(),
            // Trip A's drop-off point.
            TripWaypoint.newBuilder()
                .setLocation(tripA.getDropoffPoint())
                .setTripId(TRIP_A_ID)
                .setWaypointType(WaypointType.DROP_OFF_WAYPOINT_TYPE)
                .build(),
            // Trip B's drop-off point.
            TripWaypoint.newBuilder()
                .setLocation(tripBDropoffTerminalLocation)
                .setTripId(TRIP_B_ID)
                .setWaypointType(WaypointType.DROP_OFF_WAYPOINT_TYPE)
                .build()))
    .build();
// Create Trip request
CreateTripRequest createTripRequest = CreateTripRequest.newBuilder()
    .setParent(parent)
    .setTripId(TRIP_B_ID)
    .setTrip(tripB)
    .build();
try {
  // createdTrip.remainingWaypoints will contain shared-pool waypoints.
  // [tripB.pickup, tripA.dropoff, tripB.dropoff]
  Trip createdTrip = tripService.createTrip(createTripRequest);
} catch (StatusRuntimeException e) {
  Status s = e.getStatus();
  switch (s.getCode()) {
    case ALREADY_EXISTS:
      break;
    case PERMISSION_DENIED:
      break;
  }
  return;
}
更新了合乘行程
在 Fleet Engine 中创建的任何行程都必须分配给车辆,以便 Fleet Engine 计算行程预计到达时间并跟踪行程。您可以在创建行程期间执行此操作,也可以在稍后更新行程时执行此操作。
对于共享出行,您必须在行程的车辆途经点集合 (Trip.vehicle_waypoints) 中指定未访问途经点的顺序。Fleet Engine 会使用此列表自动更新共享出行中所有行程的途经点。
例如,假设有两趟拼车行程,分别是行程 A 和行程 B:
- 行程 A 正在前往其下车地点。
- 然后,系统会将行程 B 添加到同一车辆。
在 行程 B 的一个 UpdateTripRequest 中,您设置了 vehicleId,并将 Trip.vehicle_waypoints 设置为最佳途经点顺序:B 上车点 → A 下车点 → B 下车点。
- 调用 getVehicle()会返回包含以下内容的remainingWaypoints:
 B 上车 → A 下车 → B 下车。
- 行程 A 的 getTrip()或onTripRemainingWaypointsUpdated回调返回包含以下内容的remainingWaypoints:
 B 上车点 → A 下车点。
- 行程 B 的 getTrip()或onTripRemainingWaypointsUpdated回调返回包含以下内容的remainingWaypoints:
 B 上车点 → A 下车点 → B 下车点。
示例
以下后端集成示例演示了如何使用车辆 ID 和途经点更新两次共享出行。
static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String TRIP_A_ID = "share-trip-A";
static final String TRIP_B_ID = "share-trip-B";
static final String VEHICLE_ID = "Vehicle";
String tripName = "providers/" + PROJECT_ID + "/trips/" + TRIP_B_ID;
// Get Trip A and Trip B objects from either the Fleet Engine or storage.
Trip tripA = …;
Trip tripB = …;
TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);
// The trip settings to update.
Trip trip = Trip.newBuilder()
    .setVehicleId(VEHICLE_ID)
    .addAllVehicleWaypoints(
        // This is where you define the arrival order for unvisited waypoints.
        // If you don't specify an order, then the Fleet Engine adds Trip B's
        // waypoints to the end of Trip A's.
        ImmutableList.of(
            // Trip B's pickup point.
            TripWaypoint.newBuilder()
                .setLocation(tripB.getPickupPoint())
                .setTripId(TRIP_B_ID)
                .setWaypointType(WaypointType.PICKUP_WAYPOINT_TYPE)
                .build(),
            // Trip A's drop-off point.
            TripWaypoint.newBuilder()
                .setLocation(tripA.getDropoffPoint())
                .setTripId(TRIP_A_ID)
                .setWaypointType(WaypointType.DROP_OFF_WAYPOINT_TYPE)
                .build(),
            // Trip B's drop-off point.
            TripWaypoint.newBuilder()
                .setLocation(tripB.getDropoffPoint())
                .setTripId(TRIP_B_ID)
                .setWaypointType(WaypointType.DROP_OFF_WAYPOINT_TYPE)
                .build()))
    .build();
// The trip update request.
UpdateTripRequest updateTripRequest = UpdateTripRequest.newBuilder()
    .setName(tripName)
    .setTrip(trip)
    .setUpdateMask(FieldMask.newBuilder()
        .addPaths("vehicle_id")
        .addPaths("vehicle_waypoints"))
    .build();
// Error handling. If Fleet Engine has both a trip and vehicle with the IDs,
// and if the credentials validate, and if the given vehicle_waypoints list
// is valid, then the service updates the trip.
try {
  Trip updatedTrip = tripService.updateTrip(updateTripRequest);
} catch (StatusRuntimeException e) {
  Status s = e.getStatus();
  switch (s.getCode()) {
    case NOT_FOUND:          // Either the trip or vehicle does not exist.
      break;
    case PERMISSION_DENIED:
      break;
    case INVALID_REQUEST:    // vehicle_waypoints is invalid.
      break;
  }
  return;
}