Files
EM2_ERP/em2rp/scratch/test_js.js
T

46 lines
1.2 KiB
JavaScript

const encoded = "qospGlrrB";
function safeDecodePolyline(encoded) {
let index = 0, len = encoded.length;
let lat = 0, lng = 0;
const poly = [];
while (index < len) {
let b, shift = 0, result = 0;
do {
if (index >= len) break;
b = encoded.charCodeAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
let dlat = ((result & 1) !== 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
if (index >= len) break;
b = encoded.charCodeAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
let dlng = ((result & 1) !== 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
poly.push([lat / 1e5, lng / 1e5]);
}
return poly;
}
const pts = safeDecodePolyline(encoded);
console.log("Decoded", pts.length, "points.");
for(let i=0; i<5 && i<pts.length; i++) {
console.log(pts[i]);
}
if(pts.length > 5) {
console.log("...");
for(let i=pts.length-5; i<pts.length; i++) {
console.log(pts[i]);
}
}