All files / lib/util polyline.js

0% Statements 0/25
0% Branches 0/4
0% Functions 0/1
0% Lines 0/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                                                                         
/** TODO: use library? **/
 
export function decode(polyline) {
  let currentPosition = 0
 
  let currentLat = 0
  let currentLng = 0
 
  const dataLength = polyline.length
 
  const polylineLatLngs = []
 
  while (currentPosition < dataLength) {
    let shift = 0
    let result = 0
 
    var byte
 
    do {
      byte = polyline.charCodeAt(currentPosition++) - 63
      result |= (byte & 0x1f) << shift
      shift += 5
    } while (byte >= 0x20)
 
    const deltaLat = result & 1 ? ~(result >> 1) : result >> 1
    currentLat += deltaLat
 
    shift = 0
    result = 0
 
    do {
      byte = polyline.charCodeAt(currentPosition++) - 63
      result |= (byte & 0x1f) << shift
      shift += 5
    } while (byte >= 0x20)
 
    const deltLng = result & 1 ? ~(result >> 1) : result >> 1
 
    currentLng += deltLng
 
    polylineLatLngs.push([currentLat * 0.00001, currentLng * 0.00001])
  }
  return polylineLatLngs
}