Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Let's Write a Train Tracking Algorithm

Avatar for Chris Trott Chris Trott
September 20, 2025

Let's Write a Train Tracking Algorithm

A presentation at iOSDC 2025 Japan exploring the algorithm behind Eki Live, an iOS app that automatically detects the railway and next station for a train you're riding in Tokyo and updates it in the Dynamic Island.

Avatar for Chris Trott

Chris Trott

September 20, 2025
Tweet

More Decks by Chris Trott

Other Decks in Programming

Transcript

  1. Railway Distance from Location (m) Tokyu Toyoko 12.19 Tokyu Shin-Yokohama

    177.19 Yokohama Green 1542.94 Tokyu Meguro 2266.07
  2. Railway Algorithm V3 — Add penalty for passed stations —

    Add penalty for stopping between stations
  3. Focus Station Algorithm V1 — Calculate distance d from Location

    to closest station S — Calculate direction vector c from Location to S Case Result d < 200m "Now: S" d < 500m && c > 0 "Soon: S" c > 0 "Next: S" else "Next: S+1"
  4. struct StationDirectionalLocationHistory { let stationID: Station.ID let railDirection: RailDirection var

    visitingLocations: [Location] = [] var approachingLocations: [Location] = [] var firstDepartureLocation: Location? }
  5. Focus Station Algorithm V2 — Step 1: assign locations to

    stations — Step 2: update station phase history — Step 3: select the focus station phase
  6. Step 2: update station phase history visiting approach departure ->

    Phase isEmpty isEmpty !nil departure isEmpty !isEmpty nil approaching !isEmpty any nil visiting !isEmpty any !nil visited
  7. Step 3: determine focus phase — Find last station S

    in travel direction where phase != nil Latest Station Phase Focus Phase departure Next: S+1 approaching Soon: S visiting Now: S visited Next: S+1
  8. ✨ Hire Me ✨ — Available for full-time or contract

    work — iOS generalist (not just train apps) — twocentstudios.com/blog — @twocentstudios