/** * Decodes an encoded Polyline string into an array of [lng, lat] coordinates. * @param str The encoded polyline string * @param precision The precision of the coordinates (default 5 for Google Polyline) * @returns Array of [lng, lat] coordinates */ export function decodePolyline(str: string, precision: number = 5): [number, number][] { let index = 0; let lat = 0; let lng = 0; const coordinates: [number, number][] = []; let shift = 0; let result = 0; let byte: number | null = null; let latitude_change: number; let longitude_change: number; const factor = Math.pow(10, precision); while (index < str.length) { byte = null; shift = 0; result = 0; do { byte = str.charCodeAt(index++) - 63; result |= (byte & 0x1f) << shift; shift += 5; } while (byte >= 0x20); latitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1)); lat += latitude_change; shift = 0; result = 0; do { byte = str.charCodeAt(index++) - 63; result |= (byte & 0x1f) << shift; shift += 5; } while (byte >= 0x20); longitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1)); lng += longitude_change; coordinates.push([lng / factor, lat / factor]); } return coordinates; }