Track a Trip
Mobile Network SDK
And now that the user has successfully booked the trip, they can be offered the option to track the trip status and driver while waiting to be picked up.
Track Trip
The KarhooApi.tripService provides all the functionality to enable the users to track their trip status.
You can use KarhooApi.tripService.trackTrip
to track updates of a specified trip. It requires the trip identifier as a parameter.
let tripService = Karhoo.getTripService()
/* Polling */
// create observer
let tripObserver = Observer<TripInfo> { [weak self] result in
switch result {
case .success(let tripInfo):
print("Trip: \(tripInfo)")
case .failure(let error):
// handle error (KarhooError)
}
}
// create observable
let tripObservable = tripService.trackTrip(tripId: "1234").observable()
// subscribe observer to observable
tripObservable.subscribe(observer: tripObserver)
/* Single Response */
tripService.trackTrip(tripId: "1234").execute { [weak self] result in
switch result {
case .success(let tripInfo):
print("Trip: \(tripInfo)")
case .failure(let error):
// handle error (KarhooError)
}
}
val tripService = KarhooApi.tripService
val tripId = "1234"
// Create observer and handle payload
val tripDetailsObserver = object : Observer<Resource<TripInfo>> {
override fun onValueChanged(value: Resource<TripInfo>) {
when (value) {
is Resource.Success -> print(result.data) //Handle data
is Resource.Failure -> print(result.error)) //Handle error
}
}
}
// Start tracking with default poll time
val tripDetailsObservable = tripsService.trackTrip(tripId).observable().apply {
tripDetailsObserver?.let {
subscribe(it)
}
}
Remember to unsubscribe
Observers
when you no longer need them, such as in the unloading of a view.
// unsubscribe observer from observable
tripObservable.unsubscribe(observer: tripObserver)
// Unsubscribe from observable
tripDetailsObservable?.apply {
tripDetailsObserver?.let {
unsubscribe(it)
}
}
Track Driver
The KarhooApi.driverTrackingService can be used to track the driver’s location for a specific trip.
You can use KarhooApi.driverTrackingService.trackDriver
to track updates of a specified trip. It requires the trip identifier as a parameter.
Single request
/* Single Response */
driverTracking.trackDriver(tripId: "1234").execute { [weak self] result in
switch result {
case .success(let driverTrackingInfo):
print("Driver position: \(driverTrackingInfo.position)")
case .failure(let error):
// handle error (KarhooError)
}
}
// Single response
driverTrackingService.trackDriver(tripId).execute { result ->
when (result) {
is Resource.Success -> print(result.data) // Handle data
is Resource.Failure -> print(result.error.internalMessage) //Handle errors
}
}
Polling
let driverTracking = Karhoo.getDriverTrackingService()
// Create observer
let driverTrackingObserver = Observer<DriverTrackingInfo> { [weak self] result in
switch result {
case .success(let driverTrackingInfo):
print("Driver position: \(driverTrackingInfo.position)")
case .failure(let error):
// handle error (KarhooError)
}
}
// create observable
let driverTrackingObservable = driverTracking.trackDriver(tripId: "1234").observable()
// subscribe observer to observable
driverTrackingObservable.subscribe(observer: driverTrackingObserver)
// Create observer
val driverPositionObserver = object : Observer<Resource<DriverTrackingInfo>> {
override fun onValueChanged(value: Resource<DriverTrackingInfo>) {
when (value) {
is Resource.Success -> print(value.data)
is Resource.Failure -> print(value.error.internalMessage)
}
}
}
// Subscribe to observable with repeatable poll time
val tripId = "ID_OF_THE_TRIP_TO_TRACK"
val REPEAT_INTERVAL = 10000
val driverTrackingInfoObservable = driverTrackingService.trackDriver(tripId).observable().apply {
driverPositionObserver?.let {
subscribe(it, REPEAT_INTERVAL)
}
}
Remember to unsubscribe
Observers
when you no longer need them, such as in the unloading of a view.
// Unsubscribe from observable
driverTrackingObservable.unsubscribe(observer: driverTrackingObserver)
// Unsubscribe from observable
driverPositionObserver?.let {
driverTrackingInfoObservable?.unsubscribe(it)
}
Updated almost 4 years ago
Here is a list of available open-source projects you can explore in order to learn more about our SDKs