32 lines
1.2 KiB
Dart
32 lines
1.2 KiB
Dart
import 'package:fforte/screens/addCam/exceptions/location_disabled_exception.dart';
|
|
import 'package:fforte/screens/addCam/exceptions/location_forbidden_exception.dart';
|
|
import 'package:fforte/screens/excursion/exceptions/need_always_location_exception.dart';
|
|
import 'package:geolocator/geolocator.dart';
|
|
|
|
class GeolocatorService {
|
|
// determine live position with checks for denied permission and turned off location service
|
|
static Future<Position> deteterminePosition({bool alwaysOnNeeded = false}) async {
|
|
bool locationEnabled;
|
|
LocationPermission permissionGiven;
|
|
|
|
locationEnabled = await Geolocator.isLocationServiceEnabled();
|
|
if (!locationEnabled) {
|
|
throw LocationDisabledException();
|
|
}
|
|
|
|
permissionGiven = await Geolocator.checkPermission();
|
|
if (permissionGiven == LocationPermission.denied) {
|
|
permissionGiven = await Geolocator.requestPermission();
|
|
if (permissionGiven == LocationPermission.denied || permissionGiven == LocationPermission.deniedForever) {
|
|
throw LocationForbiddenException();
|
|
}
|
|
}
|
|
|
|
if (alwaysOnNeeded && permissionGiven != LocationPermission.always) {
|
|
throw NeedAlwaysLocation();
|
|
}
|
|
|
|
return await Geolocator.getCurrentPosition();
|
|
}
|
|
}
|