51 lines
1.5 KiB
Dart
51 lines
1.5 KiB
Dart
import 'package:geolocator/geolocator.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
class LocationService {
|
|
static Future<bool> checkLocationPermission() async {
|
|
LocationPermission permission = await Geolocator.checkPermission();
|
|
if (permission == LocationPermission.denied) {
|
|
permission = await Geolocator.requestPermission();
|
|
}
|
|
return permission == LocationPermission.whileInUse ||
|
|
permission == LocationPermission.always;
|
|
}
|
|
|
|
static Future<bool> checkCameraPermission() async {
|
|
PermissionStatus status = await Permission.camera.status;
|
|
if (status.isDenied) {
|
|
status = await Permission.camera.request();
|
|
}
|
|
return status.isGranted;
|
|
}
|
|
|
|
static Future<Position?> getCurrentPosition() async {
|
|
try {
|
|
bool hasPermission = await checkLocationPermission();
|
|
if (!hasPermission) return null;
|
|
|
|
return await Geolocator.getCurrentPosition(
|
|
desiredAccuracy: LocationAccuracy.high,
|
|
);
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static double calculateDistance(
|
|
double lat1, double lon1,
|
|
double lat2, double lon2,
|
|
) {
|
|
return Geolocator.distanceBetween(lat1, lon1, lat2, lon2);
|
|
}
|
|
|
|
static bool isWithinRange(
|
|
double currentLat, double currentLon,
|
|
double targetLat, double targetLon,
|
|
{double rangeInMeters = 50.0}
|
|
) {
|
|
double distance = calculateDistance(currentLat, currentLon, targetLat, targetLon);
|
|
return distance <= rangeInMeters;
|
|
}
|
|
}
|