เอกสารนี้อธิบายวิธีสร้างการเดินทางแบบรวมกลุ่มที่ใช้ร่วมกัน ตั้งค่าฟิลด์ที่ถูกต้อง และกำหนดให้ยานพาหนะเพื่อดำเนินการ โดยเราจะถือว่าคุณได้ตั้งค่า Fleet Engine, สร้างยานพาหนะ, มีแอปคนขับที่ใช้งานได้ และ อาจมีแอปสำหรับผู้บริโภคด้วย คุณควรคุ้นเคยกับสถานการณ์การเดินทางต่างๆ ที่มีให้สำหรับการเดินทางแบบออนดีมานด์ด้วย ดูคำแนะนำที่เกี่ยวข้องต่อไปนี้สำหรับ เรื่องดังกล่าว
- ตั้งค่า Fleet Engine
 - สร้างยานพาหนะ
 - สถานการณ์การเดินทางในภาพรวมการเดินทางแบบออนดีมานด์
 
ข้อมูลเบื้องต้นเกี่ยวกับการสร้างการเดินทาง
ส่วนนี้อธิบายรายละเอียดคำขอที่จำเป็นสำหรับการสร้างการเดินทางใน Fleet Engine คุณส่งคำขอสร้างโดยใช้ gRPC หรือ REST
ฟิลด์การเดินทาง
ใช้ช่องต่อไปนี้เพื่อสร้างการเดินทางใน Fleet Engine คุณใช้ฟิลด์ที่แตกต่างกันสำหรับทริปประเภทต่างๆ ได้ ไม่ว่าจะเป็นทริปที่มีจุดหมายเดียวหรือหลายจุดหมาย ทริปต่อเนื่อง หรือทริปที่แชร์ คุณระบุฟิลด์ที่ไม่บังคับได้เมื่อสร้างการเดินทาง หรือจะตั้งค่าในภายหลังเมื่ออัปเดตการเดินทางก็ได้
| ชื่อ | จำเป็นหรือไม่ | คำอธิบาย | 
|---|---|---|
| parent | ใช่ | สตริงที่มีรหัสโปรเจ็กต์ รหัสนี้ต้องเป็นรหัสเดียวกันกับที่ใช้ ในการผสานรวม Fleet Engine ทั้งหมด โดยมีบทบาทบัญชีบริการเดียวกัน | 
| trip_id | ใช่ | สตริงที่คุณสร้างขึ้นซึ่งระบุการเดินทางนี้โดยเฉพาะ รหัสการเดินทางมี ข้อจำกัดบางอย่างตามที่ระบุไว้ในการอ้างอิง | 
| trip_type | ใช่ | ตั้งค่า TripType เป็นค่าต่อไปนี้สำหรับประเภทการเดินทางที่คุณสร้าง
    
  | 
  
| pickup_point | ใช่ | จุดเริ่มต้นของการเดินทาง | 
| ปลายทางระดับกลาง | ใช่ | การเดินทางแบบหลายจุดหมายเท่านั้น: รายชื่อจุดหมายกลางที่คนขับแวะระหว่าง
    จุดรับและจุดส่ง เช่นเดียวกับ   | 
  
| vehicle_waypoints | ใช่ | การเดินทางแบบรวมรถเท่านั้น: ฟิลด์นี้รองรับการสลับจุดแวะพักจากการเดินทางหลายครั้ง
    ซึ่งจะมีจุดแวะพักที่เหลือทั้งหมดของยานพาหนะที่กำหนด รวมถึงจุดแวะพักรับและส่งสำหรับการเดินทางนี้ คุณตั้งค่าช่องนี้ได้
    โดยโทรหา   | 
  
| number_of_passengers | ไม่ | จำนวนผู้โดยสารในการเดินทาง | 
| dropoff_point | ไม่ | จุดหมายปลายทางของการเดินทาง | 
| vehicle_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 ใช้รายการนี้เพื่ออัปเดตจุดแวะพักของการเดินทางโดยอัตโนมัติสำหรับการเดินทางทั้งหมด
ในการรวมรถ
ตัวอย่างเช่น พิจารณาการเดินทางแบบพูลที่แชร์ 2 รายการ ได้แก่ การเดินทาง ก และ การเดินทาง ข
- การเดินทาง A กำลังมุ่งหน้าไปยังจุดส่ง
 - จากนั้นระบบจะเพิ่มการเดินทาง B ลงในยานพาหนะคันเดียวกัน
 
ใน UpdateTripRequest สำหรับการเดินทาง B
คุณตั้งค่า vehicleId และตั้งค่า Trip.vehicle_waypoints เป็นลำดับจุดอ้างอิงที่เหมาะสม
การรับที่ B
→ การส่งที่ A →
การส่งที่ B
- การเรียกใช้ 
getVehicle()จะแสดงผลremainingWaypointsซึ่งมีข้อมูลต่อไปนี้
B รับ → A ส่ง → B ส่ง getTrip()หรือ การเรียกกลับonTripRemainingWaypointsUpdatedสำหรับ Trip A จะแสดงผลremainingWaypointsที่มี
B จุดรับ → A จุดส่งgetTrip()หรือ การเรียกกลับonTripRemainingWaypointsUpdatedสำหรับ การเดินทาง B จะแสดงผลremainingWaypointsซึ่งมี
จุดรับของ B → จุดส่งของ A → จุดส่งของ B
ตัวอย่าง
ตัวอย่างการผสานรวมแบ็กเอนด์ต่อไปนี้แสดงวิธีอัปเดตการเดินทางด้วย รหัสยานพาหนะและจุดอ้างอิงสำหรับการเดินทางแบบแชร์พูล 2 รายการ
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;
}