This tutorial walks you through the process of creating a trip with a single pickup and drop-off, and then sharing that journey with a consumer.
Prerequisites
To complete this tutorial, be sure to complete the following:
Set up the Fleet Engine. For more information, see Fleet Engine: Initial setup.
Integrate your app with the Driver SDK. For more information, see Initializing the Driver SDK for Android, and Integration Guide for the Driver SDK for iOS.
Integrate your consumer-facing app with the Consumer SDK. For more information, see Getting Started with the Consumer SDK for Android, and Getting Started with the Consumer SDK for iOS.
Set up authorization tokens. For more information about authorization tokens, see Creating a JSON Web Token for authorization in the Getting Started with Fleet Engine guide, and Authentication and authorization in the Consumer SDK documentation for Fleet Engine.
Step 1. Create a vehicle in Fleet Engine
Vehicles are objects that represent the vehicles in your fleet. You must create them in Fleet Engine in order to be able to track them in the consumer app.
You can create a vehicle using either of the following two approaches:
- gRPC
- Call the
CreateVehicle()
method with theCreateVehicleRequest
request message. You must have Fleet Engine Super User privileges to callCreateVehicle()
. - REST
- Call
https://fleetengine.googleapis.com/v1/providers.vehicles.create
.
Caveats
The following caveats apply when you create a vehicle.
Be sure to set the initial vehicle state to
OFFLINE
. This ensures that Fleet Engine can discover your vehicle for trip matching.The vehicle's
provider_id
must be the same as the project ID of the Google Cloud Project that contains the Service Accounts used for calling Fleet Engine. While multiple service accounts can access the Fleet Engine for the same rideshare provider, Fleet Engine doesn't currently support service accounts from different Google Cloud Projects accessing the same vehicles.The response returned from
CreateVehicle()
contains theVehicle
instance. The instance is deleted after seven days if it has not been updated usingUpdateVehicle()
. You should callGetVehicle()
before callingCreateVehicle()
just to confirm that the vehicle doesn't already exist. IfGetVehicle()
returns aNOT_FOUND
error, then you should proceed with callingCreateVehicle()
. For more information, see Vehicles and their lifecycle.
Example
The following provider code sample demonstrates how to create a vehicle in the 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;
}
To create a Vehicle
that supports back-to-back trips, you must set the backToBackEnabled
field in the Vehicle
object passed to CreateVehicleRequest
as true
.
Vehicle vehicle = Vehicle.newBuilder()
.setVehicleState(VehicleState.OFFLINE)
.addSupportedTripTypes(TripType.EXCLUSIVE)
.setMaximumCapacity(4)
.setVehicleType(VehicleType.newBuilder().setCategory(VehicleType.Category.AUTO))
.setBackToBackEnabled(true) // Set as 'true' so vehicle can be assigned back-to-back trips.
.build();
Step 2. Enable location tracking
Location tracking refers to tracking the vehicle's location during the trip, where the driver app sends telemetry to the Fleet Engine, which contains the vehicles current location. This constantly updated stream of position information is used to convey the vehicle's progress along the trip's route. When you enable location tracking, the driver app starts sending this telemetry, at a default frequency of once every five seconds.
You enable location tracking for Android and iOS as follows:
Call the Driver SDK for Android method
enableLocationTracking()
.Set the Driver SDK for iOS boolean property
locationTrackingEnabled
toYES
.
Example
The following code example demonstrates how to enable location tracking.
Java
RidesharingVehicleReporter vehicleReporter = ...;
vehicleReporter.enableLocationTracking();
Swift
vehicleReporter.locationTrackingEnabled = true
Objective-C
_vehicleReporter.locationTrackingEnabled = YES;
Step 3. Set the vehicle's state to online
You bring a vehicle into service (that is, to make it available for use) by setting its state to online, but you cannot do this until after you have enabled location tracking.
You set the vehicle's state to online for Android and iOS as follows:
Call the Driver SDK for Android method
setVehicleState(VehicleState.ONLINE)
.Call the Driver SDK for iOS method
[vehicleReporter updateVehicleState:GMTDVehicleStateOnline]
.
Example
The following code example demonstrates how to set the vehicle's state to
ONLINE
.
Java
vehicleReporter.setVehicleState(VehicleState.ONLINE);
Swift
vehicleReporter.update(.online)
Objective-C
[_vehicleReporter updateVehicleState:GMTDVehicleStateOnline];
Step 4. Create a trip in Fleet Engine
Programmatically, a
Trip
is an object that represents a journey, and you must create one for each trip
request so that they can be matched to vehicles and then tracked.
- You can create a trip by calling the
CreateTrip()
method with theCreateTripRequest
request message.
Required attributes
The following fields are required to create a trip.
parent
- A string that includes the provider ID. This must be the same as the project ID of the Google Cloud Project that contains the Service Accounts used for calling Fleet Engine
trip_id
- A string that you create, that uniquely identifies this trip.
trip_type
- One of the
TripType
enumeration values (eitherSHARED
orEXCLUSIVE
). pickup_point
- The trip's point of origin.
When you create a trip, you can provide the number_of_passengers
,
dropoff_point
, and vehicle_id
, although these fields are not required.
When you provide a vehicle_id
, the trip contains a list of remaining
waypoints, which you can use to set the destination in the driver app.
Example
The following example demonstrates how to create a trip to the Grand Indonesia
East Mall. The trip involves two passengers, it's exclusive, and its status is
new. The provider_id
of the trip must be the same as the
project ID. In the example, the rideshare provider created the Google Cloud
Project with the project ID my-rideshare-co-gcp-project
. This
project must include a service account for calling the 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;
}
Step 5. Set the destination in the driver app
After you have paired a consumer with a driver, you must configure the trip's
destination in the driver app. You can retrieve the vehicle’s destination from
its waypoints collection, which is returned by GetTrip()
, UpdateTrip()
and
GetVehicle()
.
- You set the destination by calling the Navigation SDK for Android method
setDestination()
, or by calling the Navigation SDK for iOS methodsetDestinations()
.
For the consumer app to properly render the trip, the geographic coordinates
(LatLng) supplied to setDestination()
must match those in the trip’s
waypoint. For more information, see the tutorials
Route to a Single Destination
and
Route to Multiple Destinations.
Example
The following code sample demonstrates how to set the destination in the driver app.
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));
}
}
});
}
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;
}];
}
Step 6. Listen for trip updates in the consumer app
For Android, you can listen for data updates from a trip by obtaining a
TripModel
object from theTripModelManager
and registering aTripModelCallback
listener.For iOS, you can listen for data updates from a trip by obtaining a
GMTCTripModel
object from theGMTCTripService
and registering aGMTCTripModelSubscriber
subscriber.
A TripModelCallback
listener and GMTCTripModelSubscriber
subscriber let your app receive periodic
trip progress updates on each refresh based on the auto refresh interval.
Only values that change can trigger the callback. Otherwise, the callback
remains silent.
The TripModelCallback.onTripUpdated()
and [GMTCTripModelSubscriber didUpdateTrip]
methods are always called, regardless of any data changes.
Example 1
The following code sample demonstrates how to obtain a TripModel
from
TripModelManager
/GMTCTripService
and set a listener on it.
Java
// Start journey sharing after a trip has been created via the 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);
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];
Example 2
The following code sample demonstrates how to set up a TripModelCallback
listener and GMTCTripModelSubscriber
subscriber.
Java
/**
* Callback registered with {@link TripModel#registerTripCallback(LifecycleOwner, boolean,
* TripModelCallback)} or {@link TripModel#registerTripCallback(TripModelCallback)}.
*
* Trip related data can be temporarily unavailable at scheduled callback due to factors such as
* weak or lost mobile signal in the reporting vehicle. Value of the related field will be {@code
* null} in that scenario. Please treat it accordingly and expect {@code nonnull} data at subsequent
* callbacks.
*/
public abstract class TripModelCallback {
/**
* Called when the {@link TripStatus} is updated.
*
* @param tripInfo Current trip information.
* @param status The current status of the trip.
*/
public void onTripStatusUpdated(TripInfo tripInfo, @TripStatus int status) {}
/**
* Called when the route is updated.
*
* @param tripInfo Current trip information.
* @param route Ordered list of {@link LatLng} representing the trip vehicle's route.
*/
public void onTripActiveRouteUpdated(TripInfo tripInfo, List<LatLng> route) {}
/**
* Called when the trip's vehicle location is updated.
*
* @param tripInfo Current trip information.
* @param vehicleLocation The trip vehicle's updated location information.
*/
public void onTripVehicleLocationUpdated(
TripInfo tripInfo, @Nullable VehicleLocation vehicleLocation) {}
/**
* Called when the trip's pickup location is updated.
*
* @param tripInfo Current trip information.
* @param pickup {@link TerminalLocation} of the updated pickup location. {@code null} value means
* this data is temporarily unavailable at scheduled callback.
*/
public void onTripPickupLocationUpdated(TripInfo tripInfo, @Nullable TerminalLocation pickup) {}
/**
* Called when the vehicle's arrival time or estimated arrival time at the pickup point is
* updated. Before the vehicle arrives, it is an estimate; after, it is the actual arrival time.
*
* @param tripInfo Current trip information.
* @param timestampMillis milliseconds of UTC time since Unix epoch. {@code null} value means this
* data is temporarily unavailable at scheduled callback.
*/
public void onTripPickupUTimeUpdated(TripInfo tripInfo, @Nullable Long timestampMillis) {}
/**
* Called when the trip's dropoff location is updated.
*
* @param tripInfo Current trip information.
* @param dropoff {@link TerminalLocation} of the updated dropoff location. {@code null} value
* means this data is temporarily unavailable at scheduled callback.
*/
public void onTripDropoffLocationUpdated(TripInfo tripInfo, @Nullable TerminalLocation dropoff) {}
/**
* Called when the vehicle's arrival time or estimated arrival time at the dropoff point is
* updated. Before the vehicle arrives, it is an estimate; after, it is the actual arrival time.
*
* @param tripInfo Current trip information.
* @param timestampMillis milliseconds of UTC time since Unix epoch. {@code null} value means this
* data is temporarily unavailable at scheduled callback.
*/
public void onTripDropoffTimeUpdated(TripInfo tripInfo, @Nullable Long timestampMillis) {}
/**
* Called when the estimated arrival time of the vehicle at the next waypoint (which may or may
* not belong to the named trip) has changed.
*
* @param tripInfo Current trip information.
* @param timestampMillis milliseconds of UTC time since Unix epoch. {@code null} value means this
* data is temporarily unavailable at scheduled callback.
*/
public void onTripETAToNextWaypointUpdated(TripInfo tripInfo, @Nullable Long timestampMillis) {}
/**
* Called when the remaining distance of the active leg has changed. The active leg is the route
* from the vehicle until the next waypoint belonging to the trip.
*
* @param tripInfo Current trip information.
* @param distanceMeters The length of the remaining leg, in meters. {@code null} value means this
* data is temporarily unavailable at scheduled callback.
*/
public void onTripActiveRouteRemainingDistanceUpdated(
TripInfo tripInfo, @Nullable Integer distanceMeters) {}
/**
* Called when there is an error updating the trip state, typically due to a network connectivity
* error.
*
* @param tripInfo Current trip information.
* @param error The error.
*/
public void onTripUpdateError(TripInfo tripInfo, TripUpdateError error) {}
/**
* Called when the scheduled update has been completed and the {@link TripModelCallback} callbacks
* have been completed for any changed data. Called every time the trip is updated even if no data
* has changed. The update interval can be modified in {@link
* TripModel#setTripModelOptions(TripModelOptions)}.
*
* @param tripInfo Current trip information.
*/
public void onTripUpdated(TripInfo tripInfo) {}
/**
* Called when the trip's list of remaining waypoints has changed.
*
* @param tripInfo Current trip information.
* @param waypointList The updated list of remaining waypoints.
*/
public void onTripRemainingWaypointsUpdated(TripInfo tripInfo, List<TripWaypoint> waypointList) {}
/**
* Called when the trip's list of intermediate destinations has changed.
*
* @param tripInfo Current trip information.
* @param intermediateDestinations The updated list of intermediate destination locations.
*/
public void onTripIntermediateDestinationsUpdated(
TripInfo tripInfo, List<TerminalLocation> intermediateDestinations) {}
/**
* Called when the distance of the remaining route has changed.
*
* @param tripInfo Current trip information.
* @param distanceMeters The length of the remaining leg, in meters. {@code null} value means this
* data is temporarily unavailable at scheduled callback.
*/
public void onTripRemainingRouteDistanceUpdated(
TripInfo tripInfo, @Nullable Integer distanceMeters) {}
/**
* Called when the remaining route has changed. For example, this may be triggered when the
* remaining route traffic data has changed or when the remaining route waypoints have changed.
*
* @param tripInfo Current trip information.
* @param route Ordered list of {@link LatLng} representing the trip vehicle's route.
*/
public void onTripRemainingRouteUpdated(TripInfo tripInfo, List<LatLng> route) {}
}
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
You can access information for the trip any time as follows:
Call the Consumer SDK for Android method
TripModel.getTripInfo()
. Calling this method doesn't force a data refresh, although data still continues to be refreshed at the refresh frequency.Get the Consumer SDK for iOS property
GMTCTripModel.currentTrip
.
Step 7. Update the trip with the vehicle ID
You must configure the trip with a vehicle ID so that the Fleet Engine can track the vehicle along its route.
- You can update the trip with the vehicle ID by calling the
UpdateTrip
endpoint with anUpdateTripRequest
. Use theupdate_mask
field to specify that you are updating the vehicle ID.
Notes
If you don't specify a destination when you create the trip, you can always do it here.
If you need to change the vehicle on a trip in progress, then you must set the state of the trip back to new, and then update the trip (as you did above) with the new vehicle ID.
Example
The following code sample demonstrates how to update the trip with a vehicle ID.
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;
}
Step 8. Display the journey in the consumer app
Use the ConsumerController
object to access
the Rides and Deliveries user interface element APIs.
For more information, see Using the user interface element APIs.
Example
The following code example demonstrates how to start the journey sharing user interface.
Java
JourneySharingSession 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];
Step 9. Manage the trip state in the Fleet Engine
You specify the state of a trip using one of the
TripStatus
enumeration values. When a trip's state changes (for example, changing from
ENROUTE_TO_PICKUP
to ARRIVED_AT_PICKUP
) you must update the trip state via
the Fleet Engine. Trip state always begins with a value of NEW
, and ends with
a value of either COMPLETE
or CANCELED
. For more information, see
trip_status
.
Example
The following code sample demonstrates how to update the trip state in the Fleet 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;
}