บทแนะนำนี้จะอธิบายถึงขั้นตอนการสร้างการเดินทางด้วยบริการไปรับที่ร้านและขึ้นรถเพียงครั้งเดียว แล้วแชร์เส้นทางนั้นกับผู้บริโภค
ข้อกำหนดเบื้องต้น
หากต้องการทำให้บทแนะนำนี้จบลง คุณต้องดำเนินการต่อไปนี้
ตั้งค่า Fleet Engine ดูข้อมูลเพิ่มเติมได้ที่ Fleet Engine: การตั้งค่าเริ่มต้น
ผสานรวมแอปกับ SDK ไดรเวอร์ ดูข้อมูลเพิ่มเติมได้ที่การเริ่มต้น SDK ของไดรเวอร์สำหรับ Android และคู่มือการผสานรวมสำหรับ SDK ของไดรเวอร์สำหรับ iOS
ผสานรวมแอปที่แสดงต่อผู้ใช้เข้ากับ SDK สำหรับผู้บริโภค ดูข้อมูลเพิ่มเติมได้ที่การเริ่มต้นใช้งาน SDK สำหรับผู้บริโภคสำหรับ Android และการเริ่มต้นใช้งาน SDK สำหรับผู้บริโภคสำหรับ iOS
ตั้งค่าโทเค็นการให้สิทธิ์ ดูข้อมูลเพิ่มเติมเกี่ยวกับโทเค็นการให้สิทธิ์ได้ที่การสร้าง JSON Web Token สำหรับการให้สิทธิ์ในคู่มือเริ่มต้นใช้งาน Fleet Engine และการตรวจสอบสิทธิ์และการให้สิทธิ์ในเอกสาร Consumer SDK สำหรับ Fleet Engine
ขั้นตอนที่ 1 สร้างยานพาหนะใน Fleet Engine
ยานพาหนะคือวัตถุที่แสดงถึงยานพาหนะในยานพาหนะของคุณ คุณต้องสร้างการดำเนินการใน Fleet Engine เพื่อติดตามผู้ใช้ในแอปสำหรับผู้บริโภค
คุณสามารถสร้างยานพาหนะโดยใช้วิธีใดวิธีหนึ่งต่อไปนี้
- gRPC
- เรียกใช้เมธอด
CreateVehicle()
ด้วยข้อความคำขอCreateVehicleRequest
คุณต้องมีสิทธิ์ของผู้ใช้ Fleet Engine ระดับสูงเพื่อโทรหาCreateVehicle()
- REST
- โทร
https://fleetengine.googleapis.com/v1/providers.vehicles.create
คำเตือน
ข้อควรระวังต่อไปนี้จะปรากฏเมื่อคุณสร้างยานพาหนะ
อย่าลืมตั้งค่าสถานะเริ่มต้นของรถเป็น
OFFLINE
วิธีนี้ช่วยให้ Fleet Engine ค้นพบรถของคุณเพื่อจับคู่การเดินทางได้provider_id
ของยานพาหนะต้องตรงกับรหัสโปรเจ็กต์ของโปรเจ็กต์ Google Cloud ที่มีบัญชีบริการซึ่งใช้สำหรับการเรียกใช้ Fleet Engine แม้ว่าบัญชีบริการหลายบัญชีจะเข้าถึง Fleet Engine ของผู้ให้บริการบริการร่วมเดินทางรายเดียวกันได้ แต่ขณะนี้ Fleet Engine ยังไม่รองรับบัญชีบริการจากโปรเจ็กต์ Google Cloud ต่างๆ ที่เข้าถึงรถคันเดียวกันการตอบกลับที่แสดงผลจาก
CreateVehicle()
มีอินสแตนซ์Vehicle
ระบบจะลบอินสแตนซ์หลังจากผ่านไป 7 วันหากไม่มีการอัปเดตโดยใช้UpdateVehicle()
คุณควรโทรหาGetVehicle()
ก่อนโทรหาCreateVehicle()
เพื่อยืนยันว่าไม่มีรถอยู่แล้ว หากGetVehicle()
แสดงข้อผิดพลาดเกี่ยวกับNOT_FOUND
คุณควรโทรหาCreateVehicle()
ดูข้อมูลเพิ่มเติมได้ที่ยานพาหนะและอายุการใช้งาน
ตัวอย่าง
ตัวอย่างรหัสผู้ให้บริการต่อไปนี้จะแสดงวิธีสร้างยานพาหนะใน Fleet Engine
static final String PROJECT_ID = "my-rideshare-co-gcp-project";
VehicleServiceBlockingStub vehicleService = VehicleService.newBlockingStub(channel);
String parent = "providers/" + PROJECT_ID;
Vehicle vehicle = Vehicle.newBuilder()
.setVehicleState(VehicleState.OFFLINE) // Initial state
.addSupportedTripTypes(TripType.EXCLUSIVE)
.setMaximumCapacity(4)
.setVehicleType(VehicleType.newBuilder().setCategory(VehicleType.Category.AUTO))
.build();
CreateVehicleRequest createVehicleRequest = CreateVehicleRequest.newBuilder()
.setParent(parent)
.setVehicleId("8241890") // Vehicle ID assigned by solution provider.
.setVehicle(vehicle) // Initial state.
.build();
// The Vehicle is created in the OFFLINE state, and no initial position is
// provided. When the driver app calls the rideshare provider, the state can be
// set to ONLINE, and the driver app updates the vehicle location.
try {
Vehicle createdVehicle = vehicleService.createVehicle(createVehicleRequest);
} catch (StatusRuntimeException e) {
Status s = e.getStatus();
switch (s.getCode()) {
case ALREADY_EXISTS:
break;
case PERMISSION_DENIED:
break;
}
return;
}
ขั้นตอนที่ 2 เปิดใช้การติดตามตำแหน่ง
การติดตามตำแหน่งหมายถึงการติดตามตำแหน่งของรถในระหว่างการเดินทาง ซึ่งแอปคนขับจะส่งการวัดและส่งข้อมูลทางไกลไปยัง Fleet Engine ซึ่งมีตำแหน่งปัจจุบันของยานพาหนะ ระบบใช้สตรีมข้อมูลตำแหน่งที่อัปเดตอยู่ตลอดเวลา เพื่อถ่ายทอดความคืบหน้าของรถตลอดเส้นทางการเดินทาง เมื่อเปิดใช้การติดตามตำแหน่ง แอปคนขับจะเริ่มส่งการวัดและส่งข้อมูลทางไกลนี้ที่ความถี่เริ่มต้น 1 ครั้งทุก 5 วินาที
คุณเปิดใช้การติดตามตำแหน่งสำหรับ Android และ iOS โดยทำดังนี้
เรียกใช้ Driver SDK สำหรับเมธอด Android
enableLocationTracking()
ตั้งค่า Driver SDK สำหรับพร็อพเพอร์ตี้บูลีนของ iOS
locationTrackingEnabled
เป็นtrue
ตัวอย่าง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเปิดใช้การติดตามตําแหน่ง
Java
RidesharingVehicleReporter vehicleReporter = ...;
vehicleReporter.enableLocationTracking();
Kotlin
val vehicleReporter = ...
vehicleReporter.enableLocationTracking()
Swift
vehicleReporter.locationTrackingEnabled = true
Objective-C
_vehicleReporter.locationTrackingEnabled = YES;
ขั้นตอนที่ 3 ตั้งค่าสถานะของรถให้ออนไลน์
คุณนำยานพาหนะมารับบริการ (กล่าวคือ เพื่อให้สามารถใช้งาน) ได้โดยตั้งค่าสถานะของรถเป็นออนไลน์ แต่คุณไม่สามารถทำเช่นนั้นได้จนกว่าคุณจะเปิดใช้การติดตามตำแหน่ง
คุณตั้งค่าสถานะของรถให้ออนไลน์สําหรับ Android และ iOS ดังนี้
เรียกใช้ Driver SDK สำหรับเมธอด Android
setVehicleState(VehicleState.ONLINE)
เรียกใช้ Driver SDK สำหรับเมธอด iOS
vehicleReporter.update(.online)
ตัวอย่าง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งสถานะของรถเป็น ONLINE
Java
vehicleReporter.setVehicleState(VehicleState.ONLINE);
Kotlin
vehicleReporter.setVehicleState(VehicleState.ONLINE)
Swift
vehicleReporter.update(.online)
Objective-C
[_vehicleReporter updateVehicleState:GMTDVehicleStateOnline];
ขั้นตอนที่ 4 สร้างการเดินทางใน Fleet Engine
ในเชิงโปรแกรม Trip
คือออบเจ็กต์ที่แสดงถึงการเดินทาง และคุณต้องสร้างออบเจ็กต์นี้สำหรับคำขอการเดินทางแต่ละรายการเพื่อให้จับคู่กับยานพาหนะแล้วติดตามได้
- คุณสร้างการเดินทางได้โดยเรียกใช้เมธอด
CreateTrip()
พร้อมข้อความคำขอCreateTripRequest
แอตทริบิวต์ที่จำเป็น
ต้องระบุข้อมูลในช่องต่อไปนี้เพื่อสร้างการเดินทาง
parent
- สตริงที่มีรหัสผู้ให้บริการ ซึ่งต้องเหมือนกับรหัสโปรเจ็กต์ของโปรเจ็กต์ Google Cloud ที่มีบัญชีบริการซึ่งใช้สำหรับการเรียกใช้ Fleet Engine
trip_id
- สตริงที่คุณสร้างซึ่งระบุการเดินทางนี้แบบไม่ซ้ำ
trip_type
- ค่าแจกแจง
TripType
ค่าใดค่าหนึ่ง (SHARED
หรือEXCLUSIVE
) pickup_point
- จุดเริ่มต้นของการเดินทาง
เมื่อสร้างการเดินทาง คุณสามารถระบุ number_of_passengers
, dropoff_point
และ vehicle_id
ได้แม้ว่าจะไม่จำเป็นต้องกรอกข้อมูลในช่องเหล่านี้
เมื่อระบุ vehicle_id
การเดินทางจะมีรายการจุดอ้างอิงที่เหลืออยู่ ซึ่งใช้กำหนดจุดหมายในแอปคนขับได้
ตัวอย่าง
ตัวอย่างต่อไปนี้สาธิตวิธีการสร้างการเดินทางไปยังศูนย์การค้า Grand Indonesia East Mall การเดินทางครั้งนี้มีผู้โดยสาร 2 คน เป็นการเดินทางสุดพิเศษและมีสถานะใหม่ provider_id
ของการเดินทางต้องเหมือนกับรหัสโปรเจ็กต์ ในตัวอย่างนี้ ผู้ให้บริการบริการร่วมเดินทางได้สร้างโปรเจ็กต์ Google Cloud ด้วยรหัสโปรเจ็กต์ my-rideshare-co-gcp-project
โปรเจ็กต์นี้ต้องมีบัญชีบริการสำหรับการเรียกใช้ Fleet Engine
static final String PROJECT_ID = "my-rideshare-co-gcp-project";
TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);
// Trip initial settings.
String parent = "providers/" + PROJECT_ID;
Trip trip = Trip.newBuilder()
.setTripType(TripType.EXCLUSIVE) // Use TripType.SHARED for carpooling.
.setPickupPoint( // Grand Indonesia East Mall.
TerminalLocation.newBuilder().setPoint(
LatLng.newBuilder()
.setLatitude(-6.195139).setLongitude(106.820826)))
.setNumberOfPassengers(2)
// Provide the drop-off point if available.
.setDropoffPoint(
TerminalLocation.newBuilder().setPoint(
LatLng.newBuilder()
.setLatitude(-6.1275).setLongitude(106.6537)))
.build();
// Create trip request
CreateTripRequest createTripRequest = CreateTripRequest.newBuilder()
.setParent(parent)
.setTripId("trip-8241890") // Trip ID assigned by the provider.
.setTrip(trip) // The 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:
break;
case PERMISSION_DENIED:
break;
}
return;
}
ขั้นตอนที่ 5 ตั้งค่าปลายทางในแอปคนขับ
หลังจากจับคู่ผู้บริโภคกับคนขับแล้ว คุณต้องกำหนดค่าจุดหมายของการเดินทางในแอปคนขับ คุณเรียกข้อมูลปลายทางของรถได้จากจุดอ้างอิงจุดอ้างอิง ซึ่งจะส่งคืนโดย GetTrip()
, UpdateTrip()
และ GetVehicle()
- คุณตั้งค่าปลายทางได้ด้วยการเรียกใช้ Navigation SDK สำหรับเมธอด Android
setDestination()
หรือเรียกใช้ Navigation SDK สำหรับเมธอด iOSsetDestinations()
พิกัดทางภูมิศาสตร์ (LatLng) ที่ส่งไปยัง setDestination()
ต้องตรงกับข้อมูลในจุดอ้างอิงของการเดินทางเพื่อให้แอปสำหรับผู้บริโภคแสดงการเดินทางได้อย่างถูกต้อง ดูข้อมูลเพิ่มเติมได้ที่บทแนะนำ
กำหนดเส้นทางไปยังปลายทางเดียว
และ
เส้นทางไปยังหลายปลายทาง
ตัวอย่าง
ตัวอย่างโค้ดต่อไปนี้จะแสดงวิธีตั้งค่าปลายทางในแอปไดรเวอร์
Java
private void navigateToPlace(String placeId, RoutingOptions travelMode) {
Waypoint destination;
try {
destination = Waypoint.fromPlaceId(placeId, null);
} catch (Waypoint.UnsupportedPlaceIdException e) {
displayMessage("Error starting navigation: Place ID is not supported.");
return;
}
// Create a future to await the result of the asynchronous navigator task.
ListenableResultFuture<Navigator.RouteStatus> pendingRoute =
mNavigator.setDestination(destination, travelMode);
// Define the action to perform when the SDK has determined the route.
pendingRoute.setOnResultListener(
new ListenableResultFuture.OnResultListener<Navigator.RouteStatus>() {
@Override
public void onResult(Navigator.RouteStatus code) {
switch (code) {
case OK:
// Hide the toolbar to maximize the navigation UI.
if (getActionBar() != null) {
getActionBar().hide();
}
// Enable voice audio guidance (through the device speaker).
mNavigator.setAudioGuidance(
Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE);
// Simulate vehicle progress along the route for demo/debug builds.
if (BuildConfig.DEBUG) {
mNavigator.getSimulator().simulateLocationsAlongExistingRoute(
new SimulationOptions().speedMultiplier(5));
}
// Start turn-by-turn guidance along the current route.
mNavigator.startGuidance();
break;
// Handle error conditions returned by the navigator.
case NO_ROUTE_FOUND:
displayMessage("Error starting navigation: No route found.");
break;
case NETWORK_ERROR:
displayMessage("Error starting navigation: Network error.");
break;
case ROUTE_CANCELED:
displayMessage("Error starting navigation: Route canceled.");
break;
default:
displayMessage("Error starting navigation: "
+ String.valueOf(code));
}
}
});
}
Kotlin
private fun navigateToPlace(placeId: String, travelMode: RoutingOptions) {
val destination =
try {
Waypoint.fromPlaceId(placeId, null)
} catch (e: Waypoint.UnsupportedPlaceIdException) {
displayMessage("Error starting navigation: Place ID is not supported.")
return@navigateToPlace
}
// Create a future to await the result of the asynchronous navigator task.
val pendingRoute = mNavigator.setDestination(destination, travelMode)
// Define the action to perform when the SDK has determined the route.
pendingRoute.setOnResultListener(
object : ListenableResultFuture.OnResultListener<Navigator.RouteStatus>() {
override fun onResult(code: Navigator.RouteStatus) {
when (code) {
Navigator.RouteStatus.OK -> {
// Hide the toolbar to maximize the navigation UI.
getActionBar()?.hide()
// Enable voice audio guidance (through the device speaker).
mNavigator.setAudioGuidance(Navigator.AudioGuidance.VOICE_ALERTS_AND_GUIDANCE)
// Simulate vehicle progress along the route for demo/debug builds.
if (BuildConfig.DEBUG) {
mNavigator
.getSimulator()
.simulateLocationsAlongExistingRoute(SimulationOptions().speedMultiplier(5))
}
// Start turn-by-turn guidance along the current route.
mNavigator.startGuidance()
}
Navigator.RouteStatus.NO_ROUTE_FOUND -> {
displayMessage("Error starting navigation: No route found.")
}
Navigator.RouteStatus.NETWORK_ERROR -> {
displayMessage("Error starting navigation: Network error.")
}
Navigator.RouteStatus.ROUTE_CANCELED -> {
displayMessage("Error starting navigation: Route canceled.")
}
else -> {
displayMessage("Error starting navigation: ${code.name}")
}
}
}
}
)
}
Swift
private func startNavigation() {
let destinations = [
GMSNavigationWaypoint(
placeID: "ChIJnUYTpNASkFQR_gSty5kyoUk", title: "PCC Natural Market"),
GMSNavigationWaypoint(
placeID: "ChIJJ326ROcSkFQRBfUzOL2DSbo", title: "Marina Park"),
]
mapView.navigator?.setDestinations(destinations, callback: { routeStatus in
guard routeStatus == .OK else {
// Error starting navigation.
return
}
mapView.locationSimulator?.simulateLocationsAlongExistingRoute()
mapView.navigator?.isGuidanceActive = true
mapView.navigator?.sendsBackgroundNotifications = true
mapView.cameraMode = .following
})
}
Objective-C
- (void)startNavigation {
NSArray<GMSNavigationWaypoint *> *destinations =
@[[[GMSNavigationWaypoint alloc] initWithPlaceID:@"ChIJnUYTpNASkFQR_gSty5kyoUk"
title:@"PCC Natural Market"],
[[GMSNavigationWaypoint alloc] initWithPlaceID:@"ChIJJ326ROcSkFQRBfUzOL2DSbo"
title:@"Marina Park"]];
[_mapView.navigator setDestinations:destinations
callback:^(GMSRouteStatus routeStatus) {
if (routeStatus != GMSRouteStatusOK) {
// Error starting navigation.
return;
}
[_mapView.locationSimulator simulateLocationsAlongExistingRoute];
_mapView.navigator.guidanceActive = YES;
_mapView.navigator.sendsBackgroundNotifications = YES;
_mapView.cameraMode = GMSNavigationCameraModeFollowing;
}];
}
ขั้นตอนที่ 6 ฟังอัปเดตการเดินทางในแอปผู้บริโภค
สำหรับ Android คุณฟังการอัปเดตข้อมูลจากการเดินทางได้โดยรับออบเจ็กต์
TripModel
จากTripModelManager
และลงทะเบียน ListenerTripModelCallback
สำหรับ iOS คุณฟังข้อมูลอัปเดตจากการเดินทางได้โดยรับออบเจ็กต์
GMTCTripModel
จากGMTCTripService
และลงทะเบียนสมาชิกGMTCTripModelSubscriber
Listener TripModelCallback
และผู้สมัครใช้บริการ GMTCTripModelSubscriber
อนุญาตให้แอปของคุณได้รับการอัปเดตความคืบหน้าของการเดินทางเป็นระยะๆ ในการรีเฟรชแต่ละครั้งโดยอิงตามช่วงเวลาการรีเฟรชอัตโนมัติ
มีเพียงค่าที่เปลี่ยนแปลงเท่านั้นที่จะทริกเกอร์การเรียกกลับได้ ไม่เช่นนั้นโค้ดเรียกกลับ
จะยังคงปิดเสียงอยู่
ระบบจะเรียกใช้เมธอด TripModelCallback.onTripUpdated()
และ tripModel(_:didUpdate:updatedPropertyFields:)
เสมอ ไม่ว่าข้อมูลจะมีการเปลี่ยนแปลงหรือไม่ก็ตาม
ตัวอย่างที่ 1
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับ TripModel
จาก TripModelManager
/GMTCTripService
และตั้งค่า Listener
Java
// Start journey sharing after a trip has been created via Fleet Engine.
TripModelManager tripModelManager = consumerApi.getTripModelManager();
// Get a TripModel object.
TripModel tripModel = tripModelManager.getTripModel(tripName);
// Register a listener on the trip.
TripModelCallback tripCallback = new TripModelCallback() {
...
};
tripModel.registerTripCallback(tripCallback);
// Set the refresh interval.
TripModelOptions tripModelOptions = TripModelOptions.builder()
.setRefreshInterval(5000) // interval in milliseconds, so 5 seconds
.build();
tripModel.setTripModelOptions(tripModelOptions);
// The trip stops auto-refreshing when all listeners are unregistered.
tripModel.unregisterTripCallback(tripCallback);
Kotlin
// Start journey sharing after a trip has been created via Fleet Engine.
val tripModelManager = consumerApi.getTripModelManager()
// Get a TripModel object.
val tripModel = tripModelManager.getTripModel(tripName)
// Register a listener on the trip.
val tripCallback = TripModelCallback() {
...
}
tripModel.registerTripCallback(tripCallback)
// Set the refresh interval.
val tripModelOptions =
TripModelOptions.builder()
.setRefreshInterval(5000) // interval in milliseconds, so 5 seconds
.build()
tripModel.setTripModelOptions(tripModelOptions)
// The trip stops auto-refreshing when all listeners are unregistered.
tripModel.unregisterTripCallback(tripCallback)
Swift
let tripService = GMTCServices.shared().tripService
// Create a tripModel instance for listening for updates from the trip
// specified by the trip name.
let tripModel = tripService.tripModel(forTripName: tripName)
// Register for the trip update events.
tripModel.register(self)
// Set the refresh interval (in seconds).
tripModel.options.autoRefreshTimeInterval = 5
// Unregister for the trip update events.
tripModel.unregisterSubscriber(self)
Objective-C
GMTCTripService *tripService = [GMTCServices sharedServices].tripService;
// Create a tripModel instance for listening for updates from the trip
// specified by the trip name.
GMTCTripModel *tripModel = [tripService tripModelForTripName:tripName];
// Register for the trip update events.
[tripModel registerSubscriber:self];
// Set the refresh interval (in seconds).
tripModel.options.autoRefreshTimeInterval = 5;
// Unregister for the trip update events.
[tripModel unregisterSubscriber:self];
ตัวอย่างที่ 2
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าผู้ฟัง TripModelCallback
และสมาชิก GMTCTripModelSubscriber
Java
// Implements a callback for the trip model so your app can listen for trip
// updates from Fleet Engine.
TripModelCallback subscriber =
new TripModelCallback() {
@Override
public void onTripStatusUpdated(TripInfo tripInfo, @TripStatus int status) {
// ...
}
@Override
public void onTripActiveRouteUpdated(TripInfo tripInfo, List<LatLng> route) {
// ...
}
@Override
public void onTripVehicleLocationUpdated(
TripInfo tripInfo, @Nullable VehicleLocation vehicleLocation) {
// ...
}
@Override
public void onTripPickupLocationUpdated(
TripInfo tripInfo, @Nullable TerminalLocation pickup) {
// ...
}
@Override
public void onTripPickupTimeUpdated(TripInfo tripInfo, @Nullable Long timestampMillis) {
// ...
}
@Override
public void onTripDropoffLocationUpdated(
TripInfo tripInfo, @Nullable TerminalLocation dropoff) {
// ...
}
@Override
public void onTripDropoffTimeUpdated(TripInfo tripInfo, @Nullable Long timestampMillis) {
// ...
}
@Override
public void onTripETAToNextWaypointUpdated(
TripInfo tripInfo, @Nullable Long timestampMillis) {
// ...
}
@Override
public void onTripActiveRouteRemainingDistanceUpdated(
TripInfo tripInfo, @Nullable Integer distanceMeters) {
// ...
}
@Override
public void onTripUpdateError(TripInfo tripInfo, TripUpdateError error) {
// ...
}
@Override
public void onTripUpdated(TripInfo tripInfo) {
// ...
}
@Override
public void onTripRemainingWaypointsUpdated(
TripInfo tripInfo, List<TripWaypoint> waypointList) {
// ...
}
@Override
public void onTripIntermediateDestinationsUpdated(
TripInfo tripInfo, List<TerminalLocation> intermediateDestinations) {
// ...
}
@Override
public void onTripRemainingRouteDistanceUpdated(
TripInfo tripInfo, @Nullable Integer distanceMeters) {
// ...
}
@Override
public void onTripRemainingRouteUpdated(TripInfo tripInfo, List<LatLng> route) {
// ...
}
};
Kotlin
// Implements a callback for the trip model so your app can listen for trip
// updates from Fleet Engine.
val subscriber =
object : TripModelCallback() {
override fun onTripStatusUpdated(tripInfo: TripInfo, status: @TripStatus Int) {
// ...
}
override fun onTripActiveRouteUpdated(tripInfo: TripInfo, route: List<LatLng>) {
// ...
}
override fun onTripVehicleLocationUpdated(
tripInfo: TripInfo,
vehicleLocation: VehicleLocation?
) {
// ...
}
override fun onTripPickupLocationUpdated(tripInfo: TripInfo, pickup: TerminalLocation?) {
// ...
}
override fun onTripPickupTimeUpdated(tripInfo: TripInfo, timestampMillis: Long?) {
// ...
}
override fun onTripDropoffLocationUpdated(tripInfo: TripInfo, dropoff: TerminalLocation?) {
// ...
}
override fun onTripDropoffTimeUpdated(tripInfo: TripInfo, timestampMillis: Long?) {
// ...
}
override fun onTripETAToNextWaypointUpdated(tripInfo: TripInfo, timestampMillis: Long?) {
// ...
}
override fun onTripActiveRouteRemainingDistanceUpdated(
tripInfo: TripInfo,
distanceMeters: Int?
) {
// ...
}
override fun onTripUpdateError(tripInfo: TripInfo, error: TripUpdateError) {
// ...
}
override fun onTripUpdated(tripInfo: TripInfo) {
// ...
}
override fun onTripRemainingWaypointsUpdated(
tripInfo: TripInfo,
waypointList: List<TripWaypoint>
) {
// ...
}
override fun onTripIntermediateDestinationsUpdated(
tripInfo: TripInfo,
intermediateDestinations: List<TerminalLocation>
) {
// ...
}
override fun onTripRemainingRouteDistanceUpdated(tripInfo: TripInfo, distanceMeters: Int?) {
// ...
}
override fun onTripRemainingRouteUpdated(tripInfo: TripInfo, route: List<LatLng>) {
// ...
}
}
Swift
class TripModelSubscriber: NSObject, GMTCTripModelSubscriber {
func tripModel(_: GMTCTripModel, didUpdate trip: GMTSTrip?, updatedPropertyFields: GMTSTripPropertyFields) {
// Update the UI with the new `trip` data.
updateUI(with: trip)
...
}
func tripModel(_: GMTCTripModel, didUpdate tripStatus: GMTSTripStatus) {
// Handle trip status did change.
}
func tripModel(_: GMTCTripModel, didUpdateActiveRoute activeRoute: [GMTSLatLng]?) {
// Handle trip active route did update.
}
func tripModel(_: GMTCTripModel, didUpdate vehicleLocation: GMTSVehicleLocation?) {
// Handle vehicle location did update.
}
func tripModel(_: GMTCTripModel, didUpdatePickupLocation pickupLocation: GMTSTerminalLocation?) {
// Handle pickup location did update.
}
func tripModel(_: GMTCTripModel, didUpdateDropoffLocation dropoffLocation: GMTSTerminalLocation?) {
// Handle drop off location did update.
}
func tripModel(_: GMTCTripModel, didUpdatePickupETA pickupETA: TimeInterval) {
// Handle the pickup ETA did update.
}
func tripModel(_: GMTCTripModel, didUpdateDropoffETA dropoffETA: TimeInterval) {
// Handle the drop off ETA did update.
}
func tripModel(_: GMTCTripModel, didUpdateRemaining remainingWaypoints: [GMTSTripWaypoint]?) {
// Handle updates to the pickup, dropoff or intermediate destinations of the trip.
}
func tripModel(_: GMTCTripModel, didFailUpdateTripWithError error: Error?) {
// Handle the error.
}
func tripModel(_: GMTCTripModel, didUpdateIntermediateDestinations intermediateDestinations: [GMTSTerminalLocation]?) {
// Handle the intermediate destinations being updated.
}
...
}
Objective-C
@interface TripModelSubscriber : NSObject <GMTCTripModelSubscriber>
@end
@implementation TripModelSubscriber
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateTrip:(nullable GMTSTrip *)trip
updatedPropertyFields:(GMTSTripPropertyFields)updatedPropertyFields {
// Update the UI with the new `trip` data.
[self updateUIWithTrip:trip];
...
}
- (void)tripModel:(GMTCTripModel *)tripModel didUpdateTripStatus:(enum GMTSTripStatus)tripStatus {
// Handle trip status did change.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateActiveRoute:(nullable NSArray<GMTSLatLng *> *)activeRoute {
// Handle trip route did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateVehicleLocation:(nullable GMTSVehicleLocation *)vehicleLocation {
// Handle vehicle location did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdatePickupLocation:(nullable GMTSTerminalLocation *)pickupLocation {
// Handle pickup location did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateDropoffLocation:(nullable GMTSTerminalLocation *)dropoffLocation {
// Handle drop off location did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel didUpdatePickupETA:(NSTimeInterval)pickupETA {
// Handle the pickup ETA did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateRemainingWaypoints:(nullable NSArray<GMTSTripWaypoint *> *)remainingWaypoints {
// Handle updates to the pickup, dropoff or intermediate destinations of the trip.
}
- (void)tripModel:(GMTCTripModel *)tripModel didUpdateDropoffETA:(NSTimeInterval)dropoffETA {
// Handle the drop off ETA did update.
}
- (void)tripModel:(GMTCTripModel *)tripModel didFailUpdateTripWithError:(nullable NSError *)error {
// Handle the error.
}
- (void)tripModel:(GMTCTripModel *)tripModel
didUpdateIntermediateDestinations:
(nullable NSArray<GMTSTerminalLocation *> *)intermediateDestinations {
// Handle the intermediate destinations being updated.
}
…
@end
คุณเข้าถึงข้อมูลการเดินทางได้ทุกเมื่อดังนี้
เรียกใช้ Consumer SDK สำหรับเมธอด Android
TripModel.getTripInfo()
การเรียกใช้วิธีการนี้ไม่บังคับให้รีเฟรชข้อมูล แม้ว่าข้อมูลจะยังคงรีเฟรชตามความถี่ในการรีเฟรชดาวน์โหลดพร็อพเพอร์ตี้ Consumer SDK สำหรับ iOS
GMTCTripModel.currentTrip
ขั้นตอนที่ 7 อัปเดตการเดินทางโดยใช้รหัสยานพาหนะ
คุณต้องกำหนดค่าการเดินทางด้วยรหัสรถเพื่อให้ Fleet Engine ติดตามรถตลอดเส้นทางได้
- คุณอัปเดตการเดินทางด้วยรหัสรถยนต์ได้โดยโทรหาปลายทาง
UpdateTrip
ด้วยUpdateTripRequest
ใช้ช่องupdate_mask
เพื่อระบุว่าคุณกำลังอัปเดตรหัสยานพาหนะ
Notes
ถ้าไม่ระบุจุดหมายเมื่อสร้างการเดินทาง คุณจะสามารถระบุจุดหมายได้เสมอ
หากจำเป็นต้องเปลี่ยนยานพาหนะระหว่างการเดินทาง คุณจะต้องตั้งค่าสถานะการเดินทางกลับไปเป็นการเดินทางใหม่ จากนั้นอัปเดตการเดินทาง (อย่างที่ทำด้านบน) ด้วยรหัสยานพาหนะใหม่
ตัวอย่าง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีอัปเดตการเดินทางโดยใช้รหัสยานพาหนะ
static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String TRIP_ID = "trip-8241890";
String tripName = "providers/" + PROJECT_ID + "/trips/" + TRIP_ID;
TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);
// The trip settings to update.
Trip trip = Trip.newBuilder()
.setVehicleId("8241890")
.build();
// The trip update request.
UpdateTripRequest updateTripRequest =
UpdateTripRequest.newBuilder() // No need for the header.
.setName(tripName)
.setTrip(trip)
.setUpdateMask(FieldMask.newBuilder().addPaths("vehicle_id"))
.build();
// Error handling.
// If the Fleet Engine has both a trip and vehicle with IDs, and if the
// credentials validate, 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: // Neither the trip nor vehicle exist.
break;
case PERMISSION_DENIED:
break;
}
return;
}
ขั้นตอนที่ 8 แสดงเส้นทางในแอปผู้บริโภค
ใช้ออบเจ็กต์ ConsumerController
เพื่อเข้าถึง API องค์ประกอบอินเทอร์เฟซผู้ใช้
การโดยสารและการนำส่ง
ดูข้อมูลเพิ่มเติมได้ที่การใช้ API องค์ประกอบอินเทอร์เฟซผู้ใช้
ตัวอย่าง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเริ่มอินเทอร์เฟซผู้ใช้ของการแชร์เส้นทาง
Java
JourneySharingSession session = JourneySharingSession.createInstance(tripModel);
consumerController.showSession(session);
Kotlin
val session = JourneySharingSession.createInstance(tripModel)
consumerController.showSession(session)
Swift
let journeySharingSession = GMTCJourneySharingSession(tripModel: tripModel)
mapView.show(journeySharingSession)
Objective-C
GMTCJourneySharingSession *journeySharingSession =
[[GMTCJourneySharingSession alloc] initWithTripModel:tripModel];
[self.mapView showMapViewSession:journeySharingSession];
ขั้นตอนที่ 9 จัดการสถานะการเดินทางใน Fleet Engine
คุณระบุสถานะการเดินทางโดยใช้ค่าแจกแจง TripStatus
ค่าใดค่าหนึ่ง เมื่อสถานะของการเดินทางเปลี่ยนแปลง (เช่น เปลี่ยนจาก ENROUTE_TO_PICKUP
เป็น ARRIVED_AT_PICKUP
) คุณต้องอัปเดตสถานะการเดินทางผ่าน Fleet Engine สถานะการเดินทางจะเริ่มต้นด้วยค่า NEW
เสมอ และลงท้ายด้วยค่า COMPLETE
หรือ CANCELED
ดูข้อมูลเพิ่มเติมได้ที่ trip_status
ตัวอย่าง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีอัปเดตสถานะการเดินทางใน Flet Engine
static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String TRIP_ID = "trip-8241890";
String tripName = "providers/" + PROJECT_ID + "/trips/" + TRIP_ID;
TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);
// Trip settings to be updated.
Trip trip = Trip.newBuilder()
.setTripStatus(TripStatus.ARRIVED_AT_PICKUP)
.build();
// Trip update request
UpdateTripRequest updateTripRequest = UpdateTripRequest.newBuilder()
.setName(tripName)
.setTrip(trip)
.setUpdateMask(FieldMask.newBuilder().addPaths("trip_status"))
.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: // The given trip status is invalid.
break;
case PERMISSION_DENIED:
break;
}
return;
}