feat: (broken) implement route map and address autocomplete widgets with associated infrastructure testing scripts
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
List<List<double>> decodePolyline(String encoded) {
|
||||
List<List<double>> poly = [];
|
||||
int index = 0, len = encoded.length;
|
||||
int lat = 0, lng = 0;
|
||||
|
||||
while (index < len) {
|
||||
int b, shift = 0, result = 0;
|
||||
do {
|
||||
if (index >= len) break;
|
||||
b = encoded.codeUnitAt(index++) - 63;
|
||||
result |= (b & 0x1f) << shift;
|
||||
shift += 5;
|
||||
} while (b >= 0x20);
|
||||
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
|
||||
lat += dlat;
|
||||
|
||||
shift = 0;
|
||||
result = 0;
|
||||
do {
|
||||
if (index >= len) break;
|
||||
b = encoded.codeUnitAt(index++) - 63;
|
||||
result |= (b & 0x1f) << shift;
|
||||
shift += 5;
|
||||
} while (b >= 0x20);
|
||||
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
|
||||
lng += dlng;
|
||||
|
||||
poly.add([lat / 1e5, lng / 1e5]);
|
||||
}
|
||||
return poly;
|
||||
}
|
||||
|
||||
void main() async {
|
||||
final origin = "401 route du camping, 69850 Saint Martin en haut";
|
||||
final destination = "Salle des fêtes, Orliénas";
|
||||
|
||||
final requestBody = jsonEncode({
|
||||
"data": {
|
||||
"origin": origin,
|
||||
"destination": destination,
|
||||
"vehicleTollCategory": 2
|
||||
}
|
||||
});
|
||||
|
||||
// Since we don't have the auth token, let's bypass auth if possible, or just call Google Maps directly!
|
||||
// Wait, I can't call Google Maps directly without API_MAPS key.
|
||||
// I will read .env file.
|
||||
final envFile = File('functions/.env');
|
||||
final lines = await envFile.readAsLines();
|
||||
String apiKey = '';
|
||||
for (var line in lines) {
|
||||
if (line.startsWith('API_MAPS=')) {
|
||||
apiKey = line.split('=')[1].replaceAll('"', '');
|
||||
}
|
||||
}
|
||||
|
||||
if (apiKey.isEmpty) {
|
||||
print("API_MAPS not found");
|
||||
return;
|
||||
}
|
||||
|
||||
final url = 'https://routes.googleapis.com/directions/v2:computeRoutes';
|
||||
final client = HttpClient();
|
||||
final request = await client.postUrl(Uri.parse(url));
|
||||
request.headers.set('Content-Type', 'application/json');
|
||||
request.headers.set('X-Goog-Api-Key', apiKey);
|
||||
request.headers.set('X-Goog-FieldMask', 'routes.distanceMeters,routes.duration,routes.polyline.encodedPolyline');
|
||||
|
||||
final payload = jsonEncode({
|
||||
"travelMode": "DRIVE",
|
||||
"routingPreference": "TRAFFIC_AWARE",
|
||||
"origin": { "address": origin },
|
||||
"destination": { "address": destination }
|
||||
});
|
||||
|
||||
request.write(payload);
|
||||
final response = await request.close();
|
||||
final responseBody = await response.transform(utf8.decoder).join();
|
||||
|
||||
try {
|
||||
final json = jsonDecode(responseBody);
|
||||
final routes = json['routes'] as List;
|
||||
final polyStr = routes[0]['polyline']['encodedPolyline'];
|
||||
print("POLYLINE_START");
|
||||
print(polyStr);
|
||||
print("POLYLINE_END");
|
||||
} catch (e) {
|
||||
print("Error: $e\n$responseBody");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user