feat: (broken) implement route map and address autocomplete widgets with associated infrastructure testing scripts
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
const fs = require('fs');
|
||||
const https = require('https');
|
||||
|
||||
async function main() {
|
||||
const env = fs.readFileSync('functions/.env', 'utf8');
|
||||
const apiKey = env.split('\n').find(l => l.startsWith('API_MAPS=')).split('=')[1].replace(/"/g, '').trim();
|
||||
|
||||
const data = JSON.stringify({
|
||||
"travelMode": "DRIVE",
|
||||
"routingPreference": "TRAFFIC_AWARE",
|
||||
"origin": { "address": "401 route du camping, 69850 Saint Martin en haut" },
|
||||
"destination": { "address": "Salle des fêtes, Orliénas" }
|
||||
});
|
||||
|
||||
const options = {
|
||||
hostname: 'routes.googleapis.com',
|
||||
path: '/directions/v2:computeRoutes',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Goog-Api-Key': apiKey,
|
||||
'X-Goog-FieldMask': 'routes.distanceMeters,routes.duration,routes.polyline.encodedPolyline',
|
||||
'Content-Length': data.length
|
||||
}
|
||||
};
|
||||
|
||||
const req = https.request(options, res => {
|
||||
let body = '';
|
||||
res.on('data', d => body += d);
|
||||
res.on('end', () => {
|
||||
const json = JSON.parse(body);
|
||||
const encoded = json.routes[0].polyline.encodedPolyline;
|
||||
console.log("Encoded string length:", encoded.length);
|
||||
|
||||
// decode
|
||||
let index = 0, len = encoded.length;
|
||||
let lat = 0, lng = 0;
|
||||
const poly = [];
|
||||
while (index < len) {
|
||||
let b, shift = 0, result = 0;
|
||||
do {
|
||||
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 {
|
||||
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]);
|
||||
}
|
||||
|
||||
console.log("Decoded points:", poly.length);
|
||||
for(let i=0; i<5; i++) console.log(poly[i]);
|
||||
|
||||
// Search for exploding coordinates
|
||||
for(let i=0; i<poly.length; i++) {
|
||||
if (Math.abs(poly[i][0]) > 90 || Math.abs(poly[i][1]) > 180) {
|
||||
console.log("EXPLODED AT INDEX", i, "POINT:", poly[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.write(data);
|
||||
req.end();
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user