Every trip you take in Places becomes a line on the map. Drives and walks have looked good for a long time. Train and subway rides were the exception. They often started in a tangle at the station, or zigzagged across the city in a way no train ever could.

Places 1.2 untangles them. The tricky part was fixing only those lines, because plenty of good trips look messy too. A mountain road full of switchbacks, a ski run, a sailboat tacking into the wind and a walk where you paced outside a shop all zigzag, and every one of them is exactly what happened.

Dozens of train and subway rides, each drawn before in orange and after in white

How your phone finds you

GPS works by timing. About 30 GPS satellites circle the Earth some 20,000 kilometers up, each going around twice a day with atomic clocks on board, and each one constantly broadcasts the exact time and where it is. Your phone measures how long each signal took to arrive, which tells it how far away that satellite is. With four or more in view, it can work out where you are.

The timing has to be extremely precise. Radio travels about 30 centimeters in a billionth of a second, so a clock that is off by a millionth of a second would put you 300 meters away. Your phone also listens to Europe's Galileo, China's BeiDou, Russia's GLONASS and, in Japan, the Michibiki satellites, so it usually has plenty to choose from.

Einstein shows up here too. The satellites move fast, which makes their clocks run a little slow, and they sit high up where Earth's gravity is weaker, which makes them run a little fast. Together they gain about 38 millionths of a second a day, so the clocks are set to tick slightly slower before launch. Without that correction, GPS would drift by about 10 kilometers a day.

There is one big requirement: a clear line of sight to the sky. By the time GPS signals reach you, they are weaker than the background radio noise around them, and your phone has to pick them out. They pass through clouds and most windows, but metal, concrete and earth block them, and tall buildings bounce them around.

So your phone has backups. It also estimates where you are from nearby cell towers and Wi-Fi networks, and blends in other signals like its motion sensors, all adding up to a best guess. Those backups are much rougher than GPS. A cell tower might only place you within a few kilometers, which is fine for showing the weather and a problem for drawing a line. When rough guesses like that end up in a trip, the line gets spiky.

Trains are hard to track

In a car with open sky, GPS has an easy job, which is why drives in Places have looked good for a long time. A train is one of the hardest cases. You are in a metal carriage, under station roofs and in and out of tunnels, and a subway spends most of its time underground. The satellites drop out and the phone leans on cell towers instead. A fast train also covers a kilometer or more between saved points, so one rough guess looks perfectly ordinary next to its neighbors. On the map, each one becomes a spike out and back.

The ride that started it

In July, Anand took the train from Kansai Airport home to Kyoto, and the line left the airport as a big scribble. When we laid the points out, the tangle stretched from four to fifteen kilometers down the line, far past the station. For twelve minutes the phone jumped between cell towers, six kilometers east, then twelve west, while the train ran straight along the bay.

The first twenty kilometers of the Kansai ride, orange as stored and white as drawn now

Fixing it without draining your battery

The easy fix would be to keep GPS at full accuracy all day, which would drain your battery long before the day was over. Places is built to track your whole day on very little battery, so it works with fewer, lower quality points and makes up for it with smarter processing when your map is drawn. That makes the job harder, and it is how Places keeps its battery use so low.

We did it in two steps: first find only the lines that are broken, then smooth them out while keeping their shape.

Step 1: Find only the broken lines

Plenty of good trips zigzag, so the check only looks at trains and subways, where the rules are clearest. Walks, drives, boats and every other kind of trip are left out of it.

A train has to follow its track. It can curve, sometimes a lot, but it can't make a sharp hairpin on the spot or jump a kilometer to the side and straight back. For every train and subway ride, Places weighs:

  • sharp turns and spikes that shoot out and straight back
  • stray points around each station, in a zone that grows with the length of the ride
  • how often the line reverses on itself, and how straight it runs overall
  • how long the line is compared with the distance between its two stations
  • the speed the line would need, given how long the ride took

Just as important is avoiding false alarms. A railway that curves along a coast or loops through a city is fine. A long straight jump through a tunnel is fine too, since a fast train moves kilometers between points.

We tuned the rules by drawing hundreds of rides at once, a hundred to a page, with the old line in orange and the new one in white, so any rule that touched a good ride stood out right away.

The ride viewer we built for this, with every flagged train and subway ride, the selected ride, and both station ends up close

In the end, the rules caught only the broken ones: roughly four in ten train and subway rides, and not a single walk.

The check run across Anand's rides: walks never flagged, train and subway rides flagged and fixed, flights flagged and left alone

Step 2: Smooth them out and keep the shape

Smoothing the whole line harder would only give you a smooth scribble, and it would round off the curves that were right. So Places removes the bad points and keeps everything else. Every good point stays exactly where the phone recorded it, the stations stay put, and the line stays in one piece.

Most broken lines are broken at the ends. Near a station, Places trims the spikes and stray points inside a zone that grows with the length of the ride, since a fast train can carry its tangle many kilometers down the line. A curve out of the station keeps its shape, because only the spikes are removed. The Kansai ride went from 149 kilometers of zigzag to a clean 88.

A few are broken all the way through. On one ride on the Keihan line from Kyoto to Osaka, stray points made the trip look like 220 kilometers at 335 kilometers an hour, faster than any bullet train in Japan. Here Places rebuilds the line one gentle step at a time. It removes the sharpest kinks first, then the strays that sit far from the path of the points around them. Only where strays outnumber good points does it average a short stretch, so the clean parts of the ride keep every curve. That Keihan ride now draws as a clean 48 kilometers.

Tested on hundreds of rides

We tested every change on hundreds of rides from Anand's own history, to cover as many situations as we could: the New York subway and BART, bullet trains and local lines across Japan, flights around the world, and plenty of walks and runs. Broken train lines had to come out clean, and everything else had to stay exactly as it was.

A hundred of Anand's walks after the change, none flagged and none moved

Live now in Places 1.2

All of these improvements are live in Places 1.2! Your travels are smoothed automatically when the map draws them, so even older train trips show up cleanly on the new maps.

The Smooth option in Edit uses the new algorithms too. If a line looks especially wrong, you can recalculate it and save the result, and the trip's distance updates to match. Otherwise your recorded trips stay exactly as they were, and the cleanup happens when you view the map.

Together with the new map styles and other improvements in Places 1.2, this should give you much nicer maps and views of your travels.