algeria_transit_services 2.2.0
algeria_transit_services: ^2.2.0 copied to clipboard
An unofficial, public API to access Algeria's transportation services
example/example.dart
import 'dart:convert';
import 'package:algeria_transit_services/algeria_transit_services.dart';
/// A tour of the Massar API from Cheria (Tébessa): stops, lines, live departures, a trip plan,
/// place search and the live positions of a line's buses.
Future<void> main() async {
// Stops and the lines serving one.
final stations = await Station.getAllStations();
print('${stations.length} stops, e.g. ${stations.first.name} (${stations.first.code})');
final lines = await Station.getLines(stations.first.code);
for (final line in lines) {
print('Line ${line.number}: ${line.name}, ${line.sublines?.length ?? 0} directions');
}
// Live departures (buses tracked right now heading to the stop).
final departures = await Departures.getDepartures(stationCode: stations.first.code, numberOfDepartures: 5);
print(departures.isEmpty ? 'No bus tracked towards this stop' : 'Next bus in ${departures.first.estimatedMinutes} min');
// Trip planning, leaving now.
final plan = await TripPlanner.plan(fromLat: 35.2712, fromLng: 7.7530, toStopCode: '12002-S0011');
for (final option in plan.options) {
print('${option.tags} ${option.totalSeconds ~/ 60} min, ${option.rides} ride(s), tracked: ${option.tracked}');
}
// Place search (on submit only; Nominatim forbids search-as-you-type).
final places = await Places.search('lycée', language: 'fr');
print(places.map((p) => p.name).join(', '));
// Live positions of line 1's buses for a few messages.
final line = lines.first;
final channel = LocationWebSocket.locationChannel(line.id);
await for (final message in channel.stream.take(5)) {
final event = LocationWebSocket.locationParser(jsonDecode(message as String) as Map);
if (event is BusPosition) print('Bus ${event.busId} at ${event.lat}, ${event.long}');
}
await channel.sink.close();
}