เอกสารนี้อธิบายวิธีสร้างการเดินทางที่มีปลายทางหลายแห่ง ตั้งค่าช่องที่ถูกต้อง และมอบหมายให้ยานพาหนะดำเนินการ โดยถือว่าคุณได้ตั้งค่า 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 | ไม่ | รหัสของยานพาหนะที่กำหนดให้กับการเดินทาง |
ตัวอย่าง: สร้างการเดินทางที่มีจุดหมายหลายแห่ง
ตัวอย่างต่อไปนี้แสดงวิธีสร้างการเดินทางแบบหลายจุดหมายปลายทางพิเศษที่มีจุดรับ จุดส่ง และจุดหมายปลายทางระหว่างทาง 1 จุด
static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String TRIP_ID = "multi-destination-trip-A";
TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);
// Trip initial settings.
String parent = "providers/" + PROJECT_ID;
Trip trip = Trip.newBuilder()
.setTripType(TripType.EXCLUSIVE)
.setPickupPoint(
TerminalLocation.newBuilder().setPoint(
LatLng.newBuilder()
.setLatitude(-6.195139).setLongitude(106.820826)))
.setNumberOfPassengers(1)
.setDropoffPoint(
TerminalLocation.newBuilder().setPoint(
LatLng.newBuilder()
.setLatitude(-6.1275).setLongitude(106.6537)))
// Add the list of intermediate destinations.
.addAllIntermediateDestinations(
ImmutableList.of(
TerminalLocation.newBuilder().setPoint(
LatLng.newBuilder()
.setLatitude(-6.195139).setLongitude(106.820826)).build()))
.build();
// Create the Trip request.
CreateTripRequest createTripRequest = CreateTripRequest.newBuilder()
.setParent(parent)
.setTripId(TRIP_ID) // Trip ID assigned by the Provider server.
.setTrip(trip) // Initial state is NEW.
.build();
// Error handling.
try {
Trip createdTrip =
tripService.createTrip(createTripRequest);
} catch (StatusRuntimeException e) {
Status s = e.getStatus();
switch (s.getCode()) {
case ALREADY_EXISTS: // Trip already exists.
break;
case PERMISSION_DENIED:
break;
}
return;
}
อัปเดตการเดินทางที่มีจุดหมายหลายแห่ง
คุณต้องกำหนดค่าการเดินทางด้วยรหัสยานพาหนะเพื่อให้ Fleet Engine ติดตามยานพาหนะตามเส้นทางได้ ดูรายละเอียดเกี่ยวกับการอัปเดตการเดินทางได้ที่หัวข้ออัปเดตการเดินทางและจัดการสถานะ
หากคุณไม่ได้ระบุจุดส่งผู้โดยสารหรือจุดหมายปลายทางระหว่างทางเมื่อสร้างการเดินทาง คุณสามารถระบุได้ที่จุดนี้
ตัวอย่างการอัปเดตการเดินทาง
ตัวอย่างต่อไปนี้แสดงวิธีอัปเดตการเดินทางเพื่อเพิ่มรายการจุดหมายปลายทางระหว่างทางและตั้งค่ารหัสยานพาหนะ
static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String TRIP_ID = "multi-destination-trip-A";
String tripName = "providers/" + PROJECT_ID + "/trips/" + TRIP_ID;
TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);
// The trip settings to be updated.
Trip trip = Trip.newBuilder()
// Add the list of intermediate destinations.
.addAllIntermediateDestinations(
ImmutableList.of(
TerminalLocation.newBuilder().setPoint(
LatLng.newBuilder()
.setLatitude(-6.195139).setLongitude(106.820826)).build()))
.setVehicleId("8241890")
.build();
// The trip update request.
UpdateTripRequest updateTripRequest = UpdateTripRequest.newBuilder()
.setName(tripName)
.setTrip(trip)
.setUpdateMask(
FieldMask.newBuilder()
.addPaths("intermediate_destinations")
.addPaths("vehicle_id")
.build())
.build();
// Error handling.
try {
Trip updatedTrip =
tripService.updateTrip(updateTripRequest);
} catch (StatusRuntimeException e) {
Status s = e.getStatus();
switch (s.getCode()) {
case NOT_FOUND: // The trip doesn't exist.
break;
case PERMISSION_DENIED:
break;
}
return;
}
จัดการสถานะการเดินทางสำหรับการเดินทางที่มีจุดหมายหลายแห่ง
คุณระบุสถานะของการเดินทางโดยใช้ค่าการแจงนับ TripStatus ค่าใดค่าหนึ่ง เมื่อสถานะของการเดินทางเปลี่ยนแปลง เช่น จาก ENROUTE_TO_PICKUP เป็น ARRIVED_AT_PICKUP คุณต้องอัปเดตสถานะการเดินทางใน Fleet Engine สถานะการเดินทางจะเริ่มต้นด้วยค่า NEW เสมอ และลงท้ายด้วยค่า COMPLETE หรือ CANCELED
สำหรับการเดินทางที่มีจุดหมายหลายแห่ง นอกเหนือจากการอัปเดตสถานะการเดินทางเหมือนกับการเดินทางที่มีจุดหมายเดียวแล้ว คุณยังต้องอัปเดตข้อมูลต่อไปนี้ทุกครั้งที่ยานพาหนะไปถึงจุดหมายระหว่างทางด้วย
intermediateDestinationIndexintermediateDestinationsVersion
โดยใช้ค่าต่อไปนี้จากการแจงนับ TripStatus
ENROUTE_TO_PICKUPARRIVED_AT_PICKUPENROUTE_TO_INTERMEDIATE_DESTINATIONARRIVED_AT_INTERMEDIATE_DESTINATIONENROUTE_TO_DROPOFFCOMPLETE
ตัวอย่างการเดินทางที่มีจุดหมายปลายทางระหว่างทาง
ตัวอย่างต่อไปนี้แสดงวิธีสร้างการเดินทางแบบหลายจุดหมายที่ผ่านจุดรับแล้ว และตอนนี้กำลังเดินทางไปยังจุดหมายกลางจุดแรก
static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String TRIP_ID = "multi-destination-trip-A";
String tripName = "providers/" + PROJECT_ID + "/trips/" + TRIP_ID;
// Get the trip object from either the Fleet Engine or storage.
Trip trip = …;
TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);
// The trip settings to be updated.
Trip trip = Trip.newBuilder()
// Trip status cannot return to a previous state once it has passed.
.setTripStatus(TripStatus.ENROUTE_TO_INTERMEDIATE_DESTINATION)
// Enroute to the first intermediate destination.
.setIntermediateDestinationIndex(0)
// You must provide an intermediate_destinations_version to ensure that you
// have the same intermediate destinations list as the Fleet Engine.
.setIntermediateDestinationsVersion(
trip.getIntermediateDestinationsVersion())
.build();
// The trip update request.
UpdateTripRequest updateTripRequest = UpdateTripRequest.newBuilder()
.setName(tripName)
.setTrip(trip)
.setUpdateMask(
FieldMask.newBuilder()
.addPaths("trip_status")
.addPaths("intermediate_destination_index")
// intermediate_destinations_version must not be in the update mask.
.build())
.build();
// Error handling.
try {
Trip updatedTrip =
tripService.updateTrip(updateTripRequest);
} catch (StatusRuntimeException e) {
Status s = e.getStatus();
switch (s.getCode()) {
case NOT_FOUND: // The trip doesn't exist.
break;
case FAILED_PRECONDITION: // Either the trip status is invalid, or the
// intermediate_destinations_version doesn't
// match Fleet Engine's.
break;
case PERMISSION_DENIED:
break;
}
return;
}