37 lines
935 B
Dart
37 lines
935 B
Dart
import 'package:geolocator/geolocator.dart';
|
|
|
|
class LocationService {
|
|
Future<Position?> getCurrentLocation() async {
|
|
bool serviceEnabled;
|
|
LocationPermission permission;
|
|
|
|
serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
|
if (!serviceEnabled) {
|
|
return null;
|
|
}
|
|
|
|
permission = await Geolocator.checkPermission();
|
|
if (permission == LocationPermission.denied) {
|
|
permission = await Geolocator.requestPermission();
|
|
if (permission == LocationPermission.denied) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
if (permission == LocationPermission.deniedForever) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
const locationSettings = LocationSettings(
|
|
accuracy: LocationAccuracy.high,
|
|
timeLimit: Duration(seconds: 5),
|
|
);
|
|
return await Geolocator.getCurrentPosition(
|
|
locationSettings: locationSettings,
|
|
);
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
}
|