feat: GPS and web compatible
This commit is contained in:
48
lib/util/geolocator.dart
Normal file
48
lib/util/geolocator.dart
Normal file
@ -0,0 +1,48 @@
|
||||
import 'package:anyhow/anyhow.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
|
||||
/// Determine the current position of the device.
|
||||
///
|
||||
/// When the location services are not enabled or permissions
|
||||
/// are denied the `Future` will return an error.
|
||||
|
||||
Future<Result<()>> ensureLocationPermission() async {
|
||||
try {
|
||||
bool serviceEnabled;
|
||||
LocationPermission permission;
|
||||
|
||||
// Test if location services are enabled.
|
||||
serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
// Location services are not enabled don't continue
|
||||
// accessing the position and request users of the
|
||||
// App to enable the location services.
|
||||
return bail('Location services are disabled.');
|
||||
}
|
||||
|
||||
permission = await Geolocator.checkPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
permission = await Geolocator.requestPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
// Permissions are denied, next time you could try
|
||||
// requesting permissions again (this is also where
|
||||
// Android's shouldShowRequestPermissionRationale
|
||||
// returned true. According to Android guidelines
|
||||
// your App should show an explanatory UI now.
|
||||
return bail('Location permissions are denied');
|
||||
}
|
||||
}
|
||||
|
||||
if (permission == LocationPermission.deniedForever) {
|
||||
// Permissions are denied forever, handle appropriately.
|
||||
return bail(
|
||||
'Location permissions are permanently denied, we cannot request permissions.');
|
||||
}
|
||||
|
||||
// When we reach here, permissions are granted and we can
|
||||
// continue accessing the position of the device.
|
||||
return const Ok(());
|
||||
} catch (e) {
|
||||
return bail("Ensuring Location Permission failed: $e");
|
||||
}
|
||||
}
|
@ -26,7 +26,9 @@ String formatFeatureTitle(Feature feature) {
|
||||
return feature.type.when(
|
||||
building: () => '${feature.name} (Building)',
|
||||
lectureHall: () => '${feature.name} (Lecture Hall)',
|
||||
room: () => 'Room ${feature.name}',
|
||||
room: (number) => 'Room ${feature.building ?? "??"}/$number',
|
||||
pcPool: (number) => 'PC Pool ${feature.name}',
|
||||
foodDrink: () => '${feature.name} (Food/Drink)',
|
||||
door: (_) => 'Door',
|
||||
toilet: (type) => 'Toilet (${formatToiletType(feature.type as Toilet)})',
|
||||
stairs: (_) => 'Stairs',
|
||||
|
Reference in New Issue
Block a user