Complete example
This is the client side of a pickup: initialize, identify the user, request permission, start a trip to the store’s geofence with continuous tracking, receive approaching and arrival events, and complete or cancel the trip.trackingOptions to startTrip().
While a trip is active, the user’s context includes it, with eta.duration in minutes and eta.distance in meters:
Prerequisites
- A destination geofence for each place a trip can end. See Create geofences.
- Foreground location permission. A trip that uses the continuous preset doesn’t need background permission. See location permissions.
- On iOS, the Location updates and Background fetch background modes. On Android, the foreground service declared in your manifest. See the iOS SDK and Android SDK references.
- A stable ID for each journey, such as an order ID, and one for the user. See Handle login, logout, and user switching.
- An HTTPS endpoint for webhooks, so that your backend receives trip events.
Track pickups
A customer orders ahead and travels to one of your locations, and the order needs to be ready when they arrive. Use the order ID as the tripexternalId, the store ID as the destination geofence externalId, and trip metadata for handoff details such as a parking space.
In the Trips section of the Settings page, turn on the trip event types you consume. Set Approaching threshold (minutes), the ETA at which a trip becomes approaching, to the notice the store needs. Set Expiration, the number of hours after which a trip that nobody ended is ended automatically. Test and Live have separate settings.
Start the pickup trip
CallRadar.startTrip() when the journey begins. For an order the customer wants as soon as possible, that is when the order is confirmed. For a scheduled order, wait until the customer taps “I’m on my way”. Otherwise a customer at home five minutes from the store is already below the approaching threshold, and the order is prepared far too early.
Starting a trip is a network request, so disable the button that starts it until the callback returns. If the call fails, keep the order on its time-based path.
If your orders run through Olo, see Olo pickup and order firing. Your backend can also create a trip with Create a new trip, but the device still has to be sending locations for the trip to progress.
Choose tracking options for trips
Pass the continuous preset tostartTrip(). It sends an update about every 30 seconds, and while the iOS location indicator or the Android foreground service is active, foreground permission is enough for the trip to keep updating in the background. If the SDK was already tracking, it remembers the earlier options and restores them when the trip ends.
To change on-trip options without an app release, see On-trip tracking options. For preset values, see Tracking options.
Trip events
A trip’s
status is started, approaching, or arrived while it is active, and completed, canceled, or expired when it ends. A trip created through the Trips API can also be pending.
Receive trip events in the app with the delegate or receiver in the complete example, and on your backend by webhook. There, look up the order by trip.externalId and deduplicate on event._id.
Prepare the order from ETA and arrival, with a time-based fallback
Location improves timing when it is available. It must never be the only thing that gets an order made, because a force-quit app, a phone that is off, or a denied permission produces no updates. Decide the latest time at which preparation must start, schedule a fallback for it, and start preparing at whichever comes first. This is the backend logic in pseudocode:started straight to arrived.
When your app knows the customer has arrived before Radar detects it, for example from an “I’m here” button, update the trip. This example adds a parking space and moves the trip to arrived:
Stopping trips
End every trip explicitly. CallcompleteTrip() at handoff, not at arrival, and cancelTrip() when the order is canceled. When either call returns, the SDK restores the tracking options that were running before the trip, or stops tracking if it wasn’t tracking before.
On iOS and Android, don’t call Radar.stopTracking() after a trip unless you want all tracking off, including background tracking that was running before the trip.
If the call fails with anything other than a not-found status, for example with no network, the trip is still active on the server. Retry, or end the trip from your backend with Update a trip. A trip that nobody ends becomes expired.
Recover on app open and resume
Trip state lives on Radar’s servers and tracking state lives on the device. They drift apart when the user force-quits the app, the device restarts, or the trip ends on the server while the app is closed. Each time the app opens or returns to the foreground, calltrackOnce() and read user.trip:
Track deliveries
Here the tracked device belongs to the traveler: a driver going to one or more customer addresses. The lifecycle is the same as a pickup, with these differences:- Identity: Call
Radar.setUserId()with the driver’s ID at sign-in, and use the delivery or route ID as the tripexternalId. - Destination: A customer address usually isn’t a geofence you already have. Create one from your backend when the job is created, with Upsert a geofence, and set
deleteAfterso that it deletes itself afterwards. - End: Complete the trip when the driver confirms the drop-off, and cancel it if the job is reassigned.
- Customer-facing ETA: Store the latest
trip.etafromuser.updated_tripwebhooks for eachtrip.externalId, and serve it from your backend. The Trips API needs a secret key, so never call it from a customer’s browser or app.
Multi-leg trips
Multi-leg trips must be enabled for your project. Contact your Radar account team.
currentLegId. For address and coordinate destinations, set arrivalRadius, in meters. Set stopDuration, in minutes, so that the ETAs of later legs account for time spent at each stop. This example starts a three-stop route with the native SDKs:
Radar.updateCurrentTripLeg() with the status completed. Radar starts the next leg, and completing the final leg completes the trip. Radar follows the leg order you give it. To change the order during the route, call Radar.reorderTripLegs(). See Trip legs.
Track workers
Here you track people for a work period instead of for one journey: field service, home health, or shift work at changing sites. A trip still covers the journey to a job site. Around it, your app switches tracking on at clock-in and off at clock-out.- Clock in: Start tracking with the responsive preset, which needs background location permission.
- Journey to a site: Start a trip to the site geofence, as in a pickup. When you complete it, the SDK restores on-clock tracking, so don’t call
stopTracking(). - Arrival and departure: On your backend, use
user.entered_geofenceanduser.exited_geofencefor the site geofence. The exit event’sdurationis the time on site in minutes. - Clock out: End any active trip first, then stop tracking, because ending a trip restores the tracking that was running before it.
Update a Live Activity from trip state
On iOS, a Live Activity can show trip progress on the Lock Screen. Radar doesn’t create or update Live Activities. Your app does, with ActivityKit. Update the activity fromdidUpdateLocation(_:user:), which carries the current trip on every update, and end it when user.trip is absent or a user.stopped_trip event arrives. Set a staleDate, because updates stop when the app is suspended or force-quit.
Handle results and errors
Test the integration
- Passing case: In the Test environment, start a trip, then call
Radar.mockTracking()on iOS or Android toward the destination geofence. The Events page showsuser.started_trip,user.updated_tripwith a falling ETA,user.approaching_trip_destination, anduser.arrived_at_trip_destination. Complete the trip, and tracking returns to its earlier state. - Failing case: Start a trip, then force-quit the app. No further updates arrive, preparation starts at the fallback time, and reopening the app restores the trip UI and tracking.
Production checklist
- Destination geofences, the approaching threshold, and expiration are set in Live.
- Every way out of a journey ends the trip: handoff, cancellation, reassignment, sign-out.
- Continuous tracking runs only during a trip.
- The app reconciles trip and tracking state on open and on resume.
- A time-based fallback covers journeys that never report a location.
- iOS background modes and the Android foreground service declaration are in place.
Build with an agent
Copy this prompt into a coding agent that has access to your repository.See also
- Trips API, including the trip
delayobject for trips withscheduledArrivalAt - Tracking options and remote configuration
- Silent push, to revive tracking for an active trip that stops sending updates
- Trip analytics, for wait time and trip time on completed trips
- Olo pickup and order firing
- Webhooks