Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

818 B
Raw Blame
/** The router's line as it sends it: Google's polyline encoding at the given precision. */
export function encodePolyline(points: [number, number][], precision: number): string {
  const factor = 10 ** precision;
  let last = [0, 0];
  return points
    .map((point) => {
      const scaled = point.map((value) => Math.round(value * factor));
      const deltas = scaled.map((value, i) => value - last[i]);
      last = scaled;
      return deltas
        .map((delta) => {
          let value = delta < 0 ? ~(delta << 1) : delta << 1;
          let chunk = '';
          while (value >= 0x20) {
            chunk += String.fromCharCode((0x20 | (value & 0x1f)) + 63);
            value >>= 5;
          }
          return chunk + String.fromCharCode(value + 63);
        })
        .join('');
    })
    .join('');
}