# Untitled

unknown
dart
23 days ago
2.0 kB
2
Indexable
Never
```double haversine(double lat1, double lon1, double lat2, double lon2) {
// Log the inputs for debugging
print('Calculating distance from (\$lat1, \$lon1) to (\$lat2, \$lon2)');

double dlon = _degreesToRadians(lon2 - lon1);
double dlat = _degreesToRadians(lat2 - lat1);
double a = pow(sin(dlat / 2), 2) + cos(_degreesToRadians(lat1)) * cos(_degreesToRadians(lat2)) * pow(sin(dlon / 2), 2);
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
double r = 6371.0; // Radius of the Earth in kilometers
double distance = c * r * 1000; // Convert to meters
print('Computed distance: \$distance meters');
return distance;
}

double? safeHaversine(double? lat1, double? lon1, double? lat2, double? lon2) {
if (lat1 == null || lon1 == null || lat2 == null || lon2 == null) {
print('Null latitude/longitude provided');
return null;
}
return haversine(lat1, lon1, lat2, lon2);
}

// Calculate distance once and store it in a map to avoid recalculating
if (distance != null) {
} else {
distanceMap[alert] = double.infinity; // Assign a large value for missing data
}
}

// Sort by severity

// Further sort each severity level by distance using precomputed distances