departure monitor / v1.17.0 / one HTML file

Google Routes API, three traffic models

It works out the latest time you can leave and still arrive on time

Google Maps tells you how long the drive takes if you leave now, plus a range like "typically 22 to 50 min", and leaves the decision to you. This app takes an origin, a destination and an arrival time, asks the Routes API about departures inside your likely window, and counts down to the latest safe departure at three levels of risk. It keeps polling while you get ready and sends a notification when it is time to go. For UCLA to LAX Terminal 7 at 6:45 PM on 18 May 2026, it said leave at 6:15, 6:02 or 5:38 PM: 37 minutes between the optimistic and pessimistic answers.

UCLA to LAX Terminal 7Arrive 6:45 PM, buffer 5 min, 18 May 2026

Same trip, three answers 37 minutes apart

Each bar is the drive Google expects under one traffic model, ending at the 6:40 PM cutoff.

Read from the dashboard below. The leave-by times are a minute earlier than 6:40 minus the rounded drive, because the app steps departures in 30-second increments and requires arrival at or before the cutoff.

At a glance

Does
Counts down to the latest departure that arrives on time, at three risk levels from Google's three traffic models.
Asks
3 Routes API calls for the anchor, then up to 9 per poll; started 2 hours out for a 37-minute drive, 13 polls and 105 calls.
Cost
About $0.05 a poll and 50 cents to a dollar a trip, the article's estimate at May 2026 prices, on your own API key.
Tested
No automated tests. The evidence is one recorded live run, the 18 May 2026 trip above, and its dashboard screenshot.
Limits
Only as good as Google's forecast; driving only, one destination; needs an open desktop tab.
Build
One HTML file of vanilla JavaScript, no server or build step.

Readout

Three traffic models, three leave-by times

The risk levels come from Google. The Routes API takes a trafficModel parameter with three values, and the app sends the same request once with each: OPTIMISTIC for Relaxed, BEST_GUESS for Usual, PESSIMISTIC for Important. The spread between the answers is the "typically X to Y min" range the Maps app shows, returned as numbers.

Relaxed is for lunch with a friend who is always late; Important is for a flight or an interview, where arriving early costs little. All three stay on screen because the gap between them is information. On a calm route they land within a few minutes of each other; on the 405 at rush hour they are more than half an hour apart.

The Leave Time Optimizer dashboard monitoring a UCLA to LAX Terminal 7 trip for a 6:45 PM arrival, with three countdowns on the right: Relaxed leave at 6:15 PM, Usual at 6:02 PM, Important at 5:38 PM.
The dashboard on 18 May 2026: form and map on the left, the three countdowns on the right.

The Routes API ignores trafficModel unless routingPreference is TRAFFIC_AWARE_OPTIMAL. It returns no error, only the same duration for all three models, and the answer looks plausible.

travelMode: 'DRIVE',
routingPreference: 'TRAFFIC_AWARE_OPTIMAL',
trafficModel: trafficModel,
departureTime: departureDate.toISOString(),
The bug that took longest to find. Request body from fetchOneRouteWithModel

Sampling

It asks about the time you plan to leave

The simple version would poll "how long is the drive right now" every few minutes. At 5:00 PM, for a 6:00 PM departure, that describes the 405 at 5:00, which can be very different from the 405 at 6:00.

So the app samples future departures. On Start it sends one anchor request for a departure 20 minutes before the arrival target. The anchor's duration sizes a departure window that ends at the target minus the buffer and starts the anchor duration plus 15 minutes earlier. Each poll queries the two ends of that window and, unless one of them is within a minute of now, a third departure for now, each with all three traffic models. With under 25 minutes to the target it asks only about now. Every sample's low and high are then widened to the lowest low and highest high of the whole poll, so a window that straddles the start of rush hour reads as uncertain.

The planner below runs that schedule, using the constants and the cadence rule from the app's source, for a trip you set.

Poll planner, arrive 6:45 PMleave-time-optimizer-1.17.0.html

Start two hours before arrival and the app polls 13 times before you leave

Assumes the drive time holds steady at your Usual estimate, so the leave-by time stays put.

Leave by6:03 PM
Polls before then0
Routes API calls0
Cost at $0.05 a poll$0

Ticks are polls. The open circle is the departure time the anchor request asks about (sent once, at Start), and the shaded span is the departure window it sizes. Constants from the app: anchor 20 minutes before target, window pad 15 minutes, two window samples, now-only under 25 minutes, polls every 15 minutes while leave-by is over 60 minutes away, every 8 at 30 to 60, every 3 inside 30. The cost per poll is the article's estimate at May 2026 prices.

Each poll also goes into a history table with the low, mean and high durations and the resulting leave-by time, so you can see whether the recommendation is still moving.

Build

One file, your own API key

Routing
Google Routes API, three requests per sample with OPTIMISTIC, BEST_GUESS and PESSIMISTIC
Required setting
routingPreference: TRAFFIC_AWARE_OPTIMAL
Call budget
3 calls for the anchor, then up to 9 per poll
Poll cadence
Every 15 min when departure is over 60 min away, 8 min at 30 to 60, 3 min under 30
Alert
Browser notification plus an on-screen alert at the leave-by time
Other APIs
Maps JavaScript (map), Places classic (autocomplete), Directions (route line)
Stack
One HTML file of vanilla JavaScript that runs without a server or build step; the last destination is kept in localStorage

Limitations

What it can't do

You need your own Google Maps API key with four APIs enabled, pasted into the HTML file, because nothing is hosted. The article estimates a poll at about $0.05 of Routes API usage and a trip at 5 to 15 polls, so 50 cents to a dollar per trip at May 2026 prices.

The forecast is Google's. If the pessimistic model misses a crash that has not happened yet, the app misses it too; what it improves is the decision about when to leave.

The layout is built for a desktop browser. Below 1080px the three columns stack into one long page, with no phone-specific design. It also depends on a browser tab staying open, and a locked phone pauses the JavaScript. It handles driving only, one destination at a time.

More

The full story

The article covers the setup steps, the interface choices and the features I would add next: calendar integration, saved routes, multi-stop trips and transit.

Leave as late as you safely can