建立共用泳池行程

本文說明如何建立共乘行程、設定正確的欄位,以及將行程指派給車輛以完成行程。本文假設您已設定 Fleet Engine、建立車輛、擁有可正常運作的駕駛人應用程式,以及 (選用) 消費者應用程式。您也應熟悉隨選行程的各種行程情境。如需相關資訊,請參閱下列指南:

建立行程基本概念

本節說明在 Fleet Engine 中建立行程時所需的請求詳細資料。您可以使用 gRPC 和 REST 發出建立要求。

  • CreateTrip() 方法:gRPCREST
  • CreateTripRequest 訊息:僅限 gRPC

行程欄位

使用下列欄位在 Fleet Engine 中建立行程。你可以針對不同類型的行程使用不同欄位,例如單一或多個目的地、連續行程或共乘行程。建立行程時,您可以提供選填欄位,也可以在稍後更新行程時設定這些欄位。

行程欄位
名稱 必要/自選 說明
parent 包含專案 ID 的字串。這個 ID 必須與整個 Fleet Engine 整合中使用的 ID 相同,且具有相同的服務帳戶角色。
trip_id 您建立的字串,用於專門識別這趟行程。如參考資料所示,行程 ID 有特定限制。
trip_type 為要建立的行程類型,將 TripType 設為下列值:
  • 單一目的地:設為 SHAREDEXCLUSIVE
  • 多目的地:設為 EXCLUSIVE
  • 背對背:設為 EXCLUSIVE
  • 共用集區:設為「SHARED」。
pickup_point 行程出發地。
中途目的地

僅限多目的地行程:司機在接送之間造訪的中途目的地清單。與 dropoff_point 相同,這個欄位也可以稍後透過呼叫 UpdateTrip 設定,但多目的地行程的定義包含中途目的地。

vehicle_waypoints

僅限共乘行程:這個欄位支援交錯使用多個行程的航點。 其中包含指派車輛的所有剩餘途經點,以及這趟行程的取貨和送達途經點。您可以呼叫 CreateTripUpdateTrip 來設定這個欄位。您也可以透過 waypoints 欄位更新車輛航點,方法是呼叫 UpdateVehicle。基於隱私權考量,這項服務不會在 GetTrip 呼叫中傳回這項資訊。

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 隨即新增至同一部車輛。

在其中一個 UpdateTripRequest 中,為「行程 B」,您設定了 vehicleId,並將 Trip.vehicle_waypoints 設為最佳中途點順序:「行程 B」取貨 →「行程 A」送貨 →「行程 B」送貨

  • 呼叫 getVehicle() 會傳回包含以下內容的 remainingWaypoints
    B 上車A 下車B 下車
  • getTrip()行程 AonTripRemainingWaypointsUpdated 回呼會傳回包含以下內容的 remainingWaypoints
    B 上車A 下車
  • getTrip()Trip BonTripRemainingWaypointsUpdated 回呼會傳回包含以下內容的 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;
}

後續步驟