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 iOS 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.shared.createAsset(assetProfile: AssetProfile) { assetId in
2
// Handle success, the result is the trip id
3
} errorHandler: { error in
4
// Handle the error
5
}
6

Bind an Asset

1
AssetTracking.shared.bindAsset(assetId: String) { assetId in
2
// Handle success, the result is the trip id
3
} errorHandler: { error in
4
// Handle the error
5
}

For comprehensive details about the above methods, please refer to the iOS 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
let tripProfile = TripProfile(customId: "Your custom trip ID", name: "Trip Name",description: "A sample Trip",attributes: ["Key of attribute":"Value of attribute"])
2
AssetTracking.shared.startTrip(tripProfile: tripProfile) { tripId in
3
// Handle success, the result is the trip id
4
} errorHandler: { error in
5
// Handle success, the result is the trip id
6
7
}

Updating Trip details

1
During the trip, you can call the `updateTrip` method to update the driver's trip profile.
2
let updateTripProfile = UpdateTripProfile(name: "Edit trip name",
3
description: "Edit description",
4
attributes: ["Edit key of attributes":"Edit value of attributes"])
5
6
AssetTracking.shared.updateTrip(tripId: tripId, tripProfile: updateTripProfile) { tripId in
7
// Handle success, the result is the trip id
8
} errorHandler: { error in
9
// Handle the error
10
}

Ending a Trip

When a trip ends, such as when an order is picked up or a driver completes a delivery 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.shared.endTrip(){ tripId in
2
// Handle success, the result is the trip id
3
} errorHandler: { error in
4
// Handle the error
5
}

Retrieving Trip details

You can use the getAssetDetail method to get detailed information about a trip. If the provided trip IDis 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.shared.getTrip(tripId: tripId) { trip in
2
// Handle success, the result is the Trip
3
} errorHandler: { [weak self] error in
4
// Handle the error
5
}

Get Trip Summary

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

1
AssetTracking.shared.tripSummaray(tripId: tripId) { summary in
2
// Handle success, the result is the TripSummary
3
} errorHandler: { [weak self] error in
4
// Handle the error
5
}

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.shared.deleteTrip(tripId: tripId) { [weak self] id in
2
// Handle success, the result is the trip id
3
} errorHandler: { error in
4
// Handle the error
5
}

Listening for Callback Methods

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

Setup the delegate

1
AssetTracking.shared.delegate = self

Implemente the delegate

1
class YourViewController: UIViewController, AssetTrackingDelegate {
2
{
3
4
/// Invoked when the status of a trip changes.
5
/// - Parameters:
6
/// - tripId: The ID of the trip whose status has changed.
7
/// - status: The new status of the trip.
8
func onTripStatusChanged(tripId: String, status: TripStatus){
9
/// Do something
10
}
11
}

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.