Skip to content

feat: free drive and session state - #28

Merged
mfazekas merged 5 commits into
mainfrom
feat/free-drive
Oct 29, 2026
Merged

mfazekas merged 5 commits into
mainfrom
feat/free-drive

Conversation

@mfazekas

@mfazekas mfazekas commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Today navigation only exists between startActiveGuidance and stopNavigation. Before and after a trip addLocationListener is silent, navigation maps have no puck, and the road name and speed limit aren't available. Speed limit and current road are among the most requested features in the unofficial packages (homee #90, pawan-pk #17), and "pick a new destination after arriving" (homee #83, #69) needs idle → free drive → guidance. This is item S1, ranked first in the API gap report.

type SessionState = 'idle' | 'freeDrive' | 'activeGuidance';

Navigation.startFreeDrive(options?: { simulatedLocation?: Position; foregroundService?: boolean }): Promise<void>;
Navigation.getSessionState(): SessionState;
Navigation.addSessionStateListener(listener: (state: SessionState) => void): EventSubscription;

addLocationListener (road name, speed limit) now fires in free drive too, and navigation maps show the puck. As in the native SDKs, startFreeDrive() during guidance is how you go from guidance to free drive; after simulated guidance the simulated device stays where guidance stopped.

Decisions applied:

  1. CarPlay doesn't start free drive on connect (startFreeDriveAutomatically = false), and the free drive the end-of-route feedback starts is stopped unless the app asked for free drive. The README says the CarPlay map has no puck until the app calls startFreeDrive().
  2. Android free drive runs without the foreground service by default; startFreeDrive({ foregroundService: true }) keeps tracking in the background with the SDK notification. Live guidance still uses the service. The README covers the billing and background implications. The SDK ignores startTripSession while a session runs, so the trip session is restarted when the location source or the service changes (e.g. live free drive → simulated guidance, or guidance → free drive without the service).
  3. stopNavigation() has no options any more; the example's "Stop to free drive" calls startFreeDrive().
  4. <NavigationView> and <RNNavigationView> stop their guidance through an internal stopViewGuidance: if the guidance started from free drive, the session returns to free drive (as startFreeDrive() would, keeping foregroundService), otherwise it goes idle. If the session already left guidance, they leave it alone.
  5. Not included, possible follow-ups: simulated free drive along a line (overlaps with the simulation-speed work), and iOS pauseFreeDrive().

Billing: Mapbox bills free drive trips like guidance trips. Free drive only starts when the app calls startFreeDrive and runs until stopNavigation or a JS reload.

Platform notes:

  1. iOS uses tripSession().startFreeDrive() / setToIdle(); the session state comes from the SDK's tripSession().session publisher, so CarPlay-initiated changes are reported too.
  2. Android: startTripSession() without routes. The trip observers are registered whenever the trip session runs and unregistered when it goes idle. The routes observer ignores updates outside guidance, because registration replays the last (empty) routes and would clear a preview route. The state is set by this package's calls rather than read from NavigationSessionStateObserver, which passes through FreeDrive while guidance starts and stops.
  3. Without location permission, startFreeDrive rejects with code: 'location_permission' (checked before anything changes, so running guidance continues).
  4. simulatedLocation parks a simulated device at that position (iOS .simulation(initialLocation:); Android a replayer observer that re-pushes the point every second).

Fixed during the device pass:

  1. Android: a parked simulated device sent about 10 location events a second, because pushEvents restarts the replayer clock at the pushed event; the next point is now pushed a second later.
  2. Android: after any simulation, live free drive dropped every device location as "timestamp from the past"; live free drive now resets the trip session, as guidance already did.
  3. iOS: starting free drive removed a route preview, because the SDK republishes a nil route progress; navigation maps now ignore repeated nil progress.

Verified: yarn lint, yarn typecheck, example tsc, yarn test (63 tests), yarn docs. On the Android emulator and the iOS simulator, with the example app and a temporary debug panel (not committed) showing the session state, the location event count, road and speed limit:

  1. Free drive toggle: freeDrive, puck at the origin, road name and speed limit; again → idle, puck gone, no further location events. Both platforms.
  2. "Simulate" → "Stop to free drive": the device stays where guidance stopped, the route line goes away, the banner shows free drive. Both.
  3. "Simulate" → "Stop": idle, no location events afterwards. Both.
  4. Live free drive: with permission denied, startFreeDrive() rejects with location_permission; with permission and a mock location, events follow the location with road name and speed limit. Both.
  5. Route preview stays when free drive starts; guidance from free drive works, including live free drive → simulated guidance. Both.
  6. Android foreground service: without the option, there's no service or notification, and in the background Android throttles the location request (no events for 3 minutes); with foregroundService: true, the location foreground service and its notification run and events keep coming in the background (about 1/s for 3 minutes). Switching back to the default stops the service.
  7. <NavigationView> and <RNNavigationView>: started from free drive, they return to free drive when closed or ended (on Android, a live free drive with the service keeps the service); started from idle, they return to idle. Both.

Seen once on iOS, before the last fix and not reproduced in eight later runs: after <NavigationView> returned to free drive, location events stalled while the state was freeDrive.

Not verified: CarPlay. The CarPlay simulator was off-limits for this pass and the example app has no CarPlay scene, so the CarPlay behaviour above is unchanged from the prototype but untested.

@mfazekas mfazekas changed the title feat: free drive and session state (prototype) feat: free drive and session state Oct 28, 2026
@mfazekas
mfazekas marked this pull request as ready for review October 28, 2026 13:34
@mfazekas
mfazekas force-pushed the feat/free-drive branch 2 times, most recently from 4f19c87 to 5d83617 Compare October 29, 2026 01:10
Add Navigation.startFreeDrive, getSessionState/addSessionStateListener and
stopNavigation({ freeDrive }). Location events and the map puck now work in
free drive too. On Android the trip observers are registered whenever the
trip session runs; without location permission startFreeDrive rejects with
code location_permission. CarPlay no longer starts free drive on its own,
since free drive trips are billed.
…nce, views return to free drive

Remove stopNavigation's freeDrive option; startFreeDrive during guidance ends it
and keeps a simulated device where guidance stopped. Android free drive runs
without the foreground service unless startFreeDrive({ foregroundService: true }).
Navigation views started from free drive return to it instead of going idle.
…ustom speed

At a simulation speed other than 1x or without auto-advance, simulated guidance
runs on the package's own simulator. startFreeDrive and a view returning to
free drive didn't see that as simulating, so they asked for location
permission and switched to live locations, and the simulator would have kept
driving the route. Free drive now parks the SDK's simulation where guidance
stopped.
@mfazekas
mfazekas merged commit 69fe6a2 into main Oct 29, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant