getRoadIncidents method
- RouteDetails route
//////////// //////////// Gets road incidents (accidents, hazards, closures) along a route
Implementation
// II.D - Road Incidents Methods
///////////////
/// Gets road incidents (accidents, hazards, closures) along a route
Future<List<Map<String, dynamic>>> getRoadIncidents(RouteDetails route) async {
try {
// Check cache first
final cacheKey = _generateIncidentsCacheKey(route);
final now = DateTime.now();
if (_incidentsCache.containsKey(cacheKey) &&
now.difference(_lastIncidentsFetch) < _cacheExpiry) {
return _incidentsCache[cacheKey] as List<Map<String, dynamic>>;
}
// Format coordinates for API call
final coords = route.points.map((p) => '${p.lng},${p.lat}').join(';');
final url = Uri.https(_baseUrl, _incidentsPath, {
'access_token': accessToken,
'geometry': coords,
'geometries': 'polyline',
'overview': 'full',
});
final response = await http.get(url);
if (response.statusCode == 200) {
final data = json.decode(response.body);
final incidents = List<Map<String, dynamic>>.from(data['incidents'] ?? []);
// Cache the results
_incidentsCache[cacheKey] = incidents;
_lastIncidentsFetch = now;
return incidents;
} else {
throw Exception('Failed to load incidents: ${response.statusCode}');
}
} catch (e) {
print('Error getting road incidents: $e');
return [];
}
}