mapbox / polyline

polyline encoding and decoding in javascript
BSD 3-Clause "New" or "Revised" License
645 stars 103 forks source link

Wrong decoding of polyline #54

Closed niclashoyer closed 5 years ago

niclashoyer commented 5 years ago

I've got a somewhat simple polyline string that I want to decode (used as a route in mapbox-gl-directions):

er_iIaun{@AgE?mDBeB?{@FeH@YD_IBqCBmGBkD@uA@_B

Rendering this route with mapbox-gl-directions results in this:

Bildschirmfoto von 2019-08-17 19-48-07

Decoding the same polyline string with the Google polyline utility results in the correct polyline:

Bildschirmfoto von 2019-08-17 19-48-30

As far as I could debug, the latitude is wrong at two points, while the longitude is correct. It seems that the decoding is wrong if the latitude does not change between two points. I'd need to dig a bit further into the decoding to verify this.

niclashoyer commented 5 years ago

I think I found the problem, the following code assumes that byte is an unsigned value:

byte = str.charCodeAt(index++) - 63;

But if the latitude or longitude does not change, this evaluates to -1 which leads to wrong values down the line. I added a Math.max(..., 0) to mitigate this. See #55.

niclashoyer commented 5 years ago

Nevermind, my encoding function on the server side is wrong. It somehow generated the character < which is 62 in decimal.