getRoadHazardsInArea method

Future<List<Map<String, dynamic>>> getRoadHazardsInArea(
  1. RaliPosition center,
  2. double radiusKm
)

Gets road hazards by type in a specific area (used for map display)

Implementation

Future<List<Map<String, dynamic>>> getRoadHazardsInArea(
  RaliPosition center,
  double radiusKm
) async {
  try {
    // Create a bounding box around the center point
    // Use proper conversion through RaliPosition
    final double latRadians = center.lat * (math.pi / 180);
    final double kmInLongitudeDegree = 111.32 * math.cos(latRadians);
    final double latChange = radiusKm / 111.32;
    final double lngChange = radiusKm / kmInLongitudeDegree;

    // Create southwest and northeast corners as RaliPositions
    final RaliPosition southwest = RaliPosition(
      center.lng - lngChange,
      center.lat - latChange
    );

    final RaliPosition northeast = RaliPosition(
      center.lng + lngChange,
      center.lat + latChange
    );

    // Create the bbox string from the RaliPositions
    final bbox = [
      southwest.lng, // min longitude
      southwest.lat, // min latitude
      northeast.lng, // max longitude
      northeast.lat, // max latitude
    ].join(',');

    final url = Uri.https(_baseUrl, _incidentsPath, {
      'access_token': accessToken,
      'bbox': bbox,
    });

    final response = await http.get(url);

    if (response.statusCode == 200) {
      final data = json.decode(response.body);
      return List<Map<String, dynamic>>.from(data['incidents'] ?? []);
    } else {
      throw Exception('Failed to load hazards: ${response.statusCode}');
    }
  } catch (e) {
    print('Error getting road hazards: $e');
    return [];
  }
}