Guide

Why is my polyline in the middle of the ocean?

You've encoded a route, dropped it onto a map, and it's off the coast of Africa, or in Antarctica, or nowhere at all. It's almost always one of three small mistakes, and each leaves a recognisable fingerprint.

Where London ends up

  1. 1 London, where it should be
  2. 2 London with latitude and longitude swapped: the Indian Ocean
  3. 3 London at precision 5, read as 6: the sea off Ghana
  4. 4 Philadelphia, swapped: Antarctica

First, what's in a polyline

An encoded polyline squeezes a list of points into a short string like _p~iF~ps|U_ulLnnqC. Each point is stored as a latitude, then a longitude, multiplied by 105 (or 106) and rounded to a whole number. Each point is also stored as the difference from the one before. Nothing in the string records which order or precision was used, so the decoder has to assume. When it assumes wrong, nothing errors. The route just lands somewhere else.

1. Latitude and longitude are swapped

This is the most common one. Polylines want [latitude, longitude], but GeoJSON, KML, Shapely, PostGIS and most GIS tools store points as [longitude, latitude], x before y. Feed those coordinates straight into an encoder and every point comes out reversed.

How to spot it: the route keeps its shape but is mirrored and moved. London (51.5°N, 0.1°W) becomes 0.1°S, 51.5°E, in the Indian Ocean east of Somalia. Cities in the Americas fare worse. Philadelphia's longitude of −75° becomes a latitude, so it lands in Antarctica. Further west, Los Angeles's −118° isn't a valid latitude at all, and many libraries reject it or wrap it somewhere strange.

How to fix it: reverse each pair before encoding.

JavaScript (@mapbox/polyline)// GeoJSON is [lng, lat]; polylines want [lat, lng]
const encoded = polyline.encode(coords.map(([lng, lat]) => [lat, lng]));

// or let the library flip it for you
const encoded = polyline.fromGeoJSON(feature);
Python (polyline)encoded = polyline.encode([(lat, lng) for lng, lat in coords])
Ruby (fast-polylines)encoded = FastPolylines.encode(coords.map(&:reverse))

Already have a swapped polyline? The decoder spots it and gives you a corrected one.

2. The precision doesn't match

The number of decimal places is a convention that the encoder and decoder have to agree on. Google, OSRM and Mapbox use precision 5 by default. Valhalla uses 6, and so do OSRM and Mapbox when you ask for polyline6.

How to spot it: decode a precision 5 polyline as if it were 6, and every coordinate shrinks by a factor of ten. The route collapses towards 0°, 0°, the patch of the Gulf of Guinea that mapping folks call "Null Island". London lands in the sea just off Ghana. Go the other way, decoding a precision 6 polyline as 5, and everything grows ten times. London becomes 515°N, which doesn't exist, so the map shows nothing or something nonsensical.

How to fix it: tell the decoder (and encoder) which precision to use.

JavaScript (@mapbox/polyline)const points = polyline.decode(encoded, 6);
Python (polyline)points = polyline.decode(encoded, 6)
Ruby (fast-polylines)points = FastPolylines.decode(encoded, 6)

The decoder tries both precisions (and both orders) and suggests the one that puts your route on land. It also catches the double whammy of a precision 6 polyline that's swapped as well.

3. A backslash got eaten

Polylines use characters from ? to ~, which includes the backslash. Paste one into a string in JavaScript, JSON, Python or Ruby, and \ starts an escape sequence. Depending on the language, the backslash silently vanishes, turns the next character into something else, or makes the parser fail. When it vanishes, the decoder has no way to know.

How to spot it: this two-point polyline near Trafalgar Square, __kyHn|Wk\cL, written as "__kyHn|Wk\cL" in JavaScript becomes __kyHn|WkcL. That decodes to a single point, with no error. In longer polylines, every point after the damage shifts, so you get a spike or a route that wanders off.

How to fix it: double the backslash (\\) in string literals, or load the polyline from a file or API response instead of pasting it into code. Google's docs mention this too.

Still not sure?

Paste it in below. The decoder draws your polyline on a map, checks for all of the above, and offers a one-click fix.