建立共用泳池行程

這份文件說明如何建立共用泳池行程,以及設定正確選項 欄位並指定車輛,然後指派給車輛完成本文假設您已設定機群 建立車輛、取得可正常運作的駕駛應用程式 也可以選擇購買消費者應用程式你應該也已經熟悉 以量計價行程可用的情境。請參閱下列相關指南: 顯示:

行程建立基本資訊

本節將說明建立行程時必要的要求詳細資料 機群引擎您可以使用 gRPC 和 REST 發出建立要求。

  • CreateTrip() 方法:gRPCREST
  • CreateTripRequest 訊息:僅限 gRPC
,瞭解如何調查及移除這項存取權。

行程欄位

請使用下列欄位在 Fleet Engine 中建立行程。不同的是 ] 欄位包含一或多個目的地 往返共乘或共享泳池行程。個人中心 可以在建立行程時提供選填欄位,你也可以自行設定 稍後更新行程時即可派上用場

行程欄位
名稱 必要/自選 說明
父項 包含專案 ID 的字串。這組 ID 必須與使用的 ID 相同 以同一個服務帳戶,全面整合 Fleet Engine 服務 角色。
trip_id 您建立的字串,可明確識別這趟行程。行程 ID 必須包含 相關限制。
trip_type 針對您要建立的行程類型,將 TripType 設為下列值:
  • 單一目的地:設為 SHAREDEXCLUSIVE
  • 多重目的地:設為 EXCLUSIVE
  • Back-to-back (返回):設為 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)。機群 引擎會根據這份清單自動更新所有行程的行程路線控點 。

舉例來說,假設有兩個共用集區行程:Trip A行程 B

  • 「行程 A」是指前往下車地點的路線。
  • 接著,行程 B 會新增至同一輛車。

在一個 UpdateTripRequest 的「Trip B」中, 設定 vehicleId,並將 Trip.vehicle_waypoints 設為最佳 路線控點順序:B 取貨放棄B 放棄

  • 呼叫 getVehicle() 會傳回 remainingWaypoints 包含:
    B 取貨放棄B 放棄
  • getTrip()onTripRemainingWaypointsUpdated 回呼: Trip A 會傳回 remainingWaypoints 包含:
    B 取貨放棄
  • getTrip()onTripRemainingWaypointsUpdated 回呼: Trip B 會傳回 remainingWaypoints 包含:
    B 取貨一則放棄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;
}

後續步驟