first commit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import 'dart:math';
|
||||
|
||||
/// أدوات حساب المسافات والاتجاهات الجغرافية
|
||||
class GeoUtils {
|
||||
static const double _earthRadiusMeters = 6371000;
|
||||
|
||||
/// Haversine formula - حساب المسافة بين نقطتين
|
||||
static double haversineDistance(
|
||||
double lat1, double lng1,
|
||||
double lat2, double lng2,
|
||||
) {
|
||||
final dLat = _toRad(lat2 - lat1);
|
||||
final dLng = _toRad(lng2 - lng1);
|
||||
final a = sin(dLat / 2) * sin(dLat / 2) +
|
||||
cos(_toRad(lat1)) * cos(_toRad(lat2)) *
|
||||
sin(dLng / 2) * sin(dLng / 2);
|
||||
final c = 2 * atan2(sqrt(a), sqrt(1 - a));
|
||||
return _earthRadiusMeters * c;
|
||||
}
|
||||
|
||||
/// Calculate bearing between two points (degrees from north)
|
||||
static double bearing(
|
||||
double lat1, double lng1,
|
||||
double lat2, double lng2,
|
||||
) {
|
||||
final dLng = _toRad(lng2 - lng1);
|
||||
final y = sin(dLng) * cos(_toRad(lat2));
|
||||
final x = cos(_toRad(lat1)) * sin(_toRad(lat2)) -
|
||||
sin(_toRad(lat1)) * cos(_toRad(lat2)) * cos(dLng);
|
||||
return (_toDeg(atan2(y, x)) + 360) % 360;
|
||||
}
|
||||
|
||||
/// Calculate speed between two points (m/s)
|
||||
static double speed(
|
||||
double lat1, double lng1, DateTime t1,
|
||||
double lat2, double lng2, DateTime t2,
|
||||
) {
|
||||
final dist = haversineDistance(lat1, lng1, lat2, lng2);
|
||||
final seconds = t2.difference(t1).inMilliseconds / 1000.0;
|
||||
if (seconds <= 0) return 0;
|
||||
return dist / seconds;
|
||||
}
|
||||
|
||||
static double _toRad(double deg) => deg * pi / 180;
|
||||
static double _toDeg(double rad) => rad * 180 / pi;
|
||||
}
|
||||
Reference in New Issue
Block a user