Tracking Trips

Trip Tracking is a powerful module designed for applications that need to track drivers' routes in real-time. By integrating the SDK into your application, you can record and manage the entire journey of a driver from start to finish.

Getting Started

Install the SDK

Before using the trip feature, you need to install the Android Tracking SDK into your project. Please refer to Getting Started for a step-by-step guide.

Configure an Asset

Next, we will create an asset and bind it to a GPS device so that we can track the asset as it moves through its Trip.

Create an Asset

1
AssetTracking.instance.createNewAsset(assetProfile, callback)

Bind an Asset

1
AssetTracking.instance.bindAsset(context, assetId, callback)

For detailed methods, refer to the Android SDK documents.

Implementing Trip Tracking

The trip feature in the Tracking SDK allows you to monitor and manage trips for your assets. This feature provides functionality for starting, updating, ending, and deleting trips, as well as retrieving trip summaries and details.

Starting a Trip

When a driver starts a trip, call the startTrip method. This will record the start time and the starting location of the trip.

1
val tripProfile = TripProfile(
2
name = "A Sample Trip",
3
description = "This is a sample trip",
4
attributes = mapOf("keyOfAttribute" to "value of attribute"),
5
metaData = mapOf("keyOfMetaData" to "value of meta data"),
6
stops = listOf(
7
TripStop(
8
name = "Trip stops",
9
metaData = mapOf("keyOfMetaData" to "value of trip stop meta data"),
10
geofenceId = "ID of geofence"
11
)
12
)
13
)
14
15
AssetTracking.instance.startTrip(this,tripProfile,true, object : AssetApiCallback<String> {
16
override fun onSuccess(result: String) {
17
// Handle success, the result is the trip id
18
}
19
20
override fun onFailure(exception: AssetException) {
21
// Handle error
22
}
23
})

You can also use the Context extension method:

1
2
assetTrackingStartTrip(tripProfile, true, object : AssetApiCallback<String> {
3
override fun onSuccess(result: String) {
4
// Handle success, the result is the trip id
5
}
6
7
override fun onFailure(exception: AssetException) {
8
// Handle error
9
}
10
})

Updating a Trip

During the trip, you can call the updateTrip method to update the driver's trip profile.

1
val tripProfile = TripUpdateProfile(
2
name = "Update Sample Trip",
3
description = "This is a sample trip that has been updated",
4
attributes = mapOf("keyOfAttribute" to "value of attribute"),
5
metaData = mapOf("keyOfMetaData" to "value of meta data"),
6
stops = listOf(
7
TripStop(
8
name = "updated stops",
9
metaData = mapOf("keyOfMetaData" to "value of trip stop meta data"),
10
geofenceId = "ID of geofence"
11
)
12
)
13
)
14
AssetTracking.instance.updateTrip(this, tripProfile, object : AssetApiCallback<String> {
15
override fun onSuccess(result: String) {
16
// Handle success, the result is the trip id
17
}
18
19
override fun onFailure(exception: AssetException) {
20
// Handle error
21
}
22
})

You can also use the Context extension method:

1
assetTrackingUpdateTrip(tripProfile, object : AssetApiCallback<String> {
2
override fun onSuccess(result: String) {
3
// Handle success, the result is the trip id
4
}
5
6
override fun onFailure(exception: AssetException) {
7
// Handle error
8
}
9
})

Ending a Trip

When a trip ends, such as when the driver reaches the destination or completes the last delivery of his route, you can mark a trip as completed, by calling the endTrip method.

1
AssetTracking.instance.endTrip(this, object : AssetApiCallback<String> {
2
override fun onSuccess(result: String) {
3
// Handle success, the result is the trip id
4
}
5
6
override fun onFailure(exception: AssetException) {
7
// Handle error
8
}
9
})

You can also use the Context extension method:

1
assetTrackingEndTrip(object : AssetApiCallback<String> {
2
override fun onSuccess(result: String) {
3
// Handle success, the result is the trip id
4
}
5
6
override fun onFailure(exception: AssetException) {
7
// Handle error
8
}
9
})

Retrieving Trip details

You can use the getTripInfo method to get detailed information about a trip. If the provided trip ID is null, details of the ongoing trip are returned. Otherwise, if a valid trip ID is provided, details of the given trip are returned.

1
AssetTracking.instance.getTripInfo(this, null, object : AssetApiCallback<Trip> {
2
override fun onSuccess(result: Trip) {
3
// Handle success, the result is the trip object
4
}
5
6
override fun onFailure(exception: AssetException) {
7
// Handle error
8
}
9
})

You can also use the Context extension method:

1
assetTrackingGetTripInfo(null, object : AssetApiCallback<Trip> {
2
override fun onSuccess(result: Trip) {
3
setupView(result)
4
}
5
6
override fun onFailure(exception: AssetException) {
7
// Handle error
8
}
9
})

Get Trip Summary

Invoke tripSummary function to get the summary of a completed trip.

1
AssetTracking.instance.tripSummary(this, tripId, object : AssetApiCallback<TripSummary> {
2
override fun onSuccess(result: TripSummary) {
3
// Handle the trip summary
4
}
5
6
override fun onFailure(exception: AssetException) {
7
// Handle the error
8
}
9
})

You can also use the Context extension method:

1
assetTrackingTripSummary(tripId = "<A_valid_trip_ID>", object : AssetApiCallback<TripSummary> {
2
override fun onSuccess(result: TripSummary) {
3
// Handle the trip summary
4
bindData(result)
5
}
6
7
override fun onFailure(exception: AssetException) {
8
// Handle the error
9
}
10
})

Deleting a Trip

You can delete a trip by providing the trip ID. However, note that once a trip is deleted, calling any other trip-related methods will no longer succeed.

1
AssetTracking.instance.deleteTrip(this, tripId, object : AssetApiCallback<String> {
2
override fun onSuccess(result: String) {
3
// Handle success, the result is the trip id
4
}
5
6
override fun onFailure(exception: AssetException) {
7
// Handle the error
8
}
9
})

You can also use the Context extension method:

1
assetTrackingDeleteTrip(tripId, object : AssetApiCallback<String> {
2
override fun onSuccess(result: String) {
3
// Handle success, the result is the trip id
4
}
5
6
override fun onFailure(exception: AssetException) {
7
// Handle the error
8
}
9
})

Listening for Callback Methods

In addition to handling success or failure in method callbacks, you can also handle trip status changes by listening for callbacks.

Adding a Callback Listener

1
val assetCallback = object : AssetTrackingCallBack {
2
// Other methods
3
...
4
5
/**
6
* Invoked when the trip status changes.
7
* @param tripId the trip id
8
* @param status the trip status, see [TripStatus]
9
*/
10
override fun onTripStatusChanged(tripId: String, status: TripStatus) {
11
// Handle the trip status change
12
}
13
}
14
15
16
assetTrackingAddCallback(assetCallback)

Removing a Callback Listener

1
assetTrackingRemoveCallback(assetCallback)

By following these methods, you can easily integrate and use the Trip Tracking feature to achieve real-time tracking and management of driver trips.

© 2024 NextBillion.ai all rights reserved.