setLocationSettings function

Future<void> setLocationSettings({
  1. bool askForPermission = true,
  2. String rationaleMessageForPermissionRequest = 'The app needs to access your location',
  3. String rationaleMessageForGPSRequest = 'The app needs to access your location',
  4. bool useGooglePlayServices = true,
  5. bool askForGooglePlayServices = false,
  6. bool askForGPS = true,
  7. bool fallbackToGPS = true,
  8. bool ignoreLastKnownPosition = true,
  9. double? expirationDuration,
  10. double? expirationTime,
  11. double fastestInterval = 500,
  12. double interval = 1000,
  13. double? maxWaitTime,
  14. int? numUpdates,
  15. LocationAccuracy accuracy = LocationAccuracy.high,
  16. double smallestDisplacement = 0,
  17. bool waitForAccurateLocation = true,
  18. double? acceptableAccuracy,
})

Update global location settings. The settings are a pass through to the LocationSettings class.

Implementation

Future<void> setLocationSettings({
  /// If set to true, the user will be prompted to grant
  /// permission to use location
  /// if not already granted.
  bool askForPermission = true,

  /// The message to display to the user when asking for permission to use
  /// location.
  /// Only valid on Android.
  /// For iOS, you have to change the permission in the Info.plist file.
  String rationaleMessageForPermissionRequest =
      'The app needs to access your location',

  /// The message to display to the user when asking for permission to use GPS.
  /// Only valid on Android.
  String rationaleMessageForGPSRequest =
      'The app needs to access your location',

  /// If set to true, the app will use Google Play Services to request location.
  /// If not available on the device, the app will fallback to GPS.
  /// Only valid on Android.
  bool useGooglePlayServices = true,

  /// If set to true, the app will request Google Play Services to request
  /// location.
  /// If not available on the device, the app will fallback to GPS.
  bool askForGooglePlayServices = false,

  /// If set to true, the app will request GPS to request location.
  /// Only valid on Android.
  bool askForGPS = true,

  /// If set to true, the app will fallback to GPS if Google Play Services
  /// is not
  /// available on the device.
  /// Only valid on Android.
  bool fallbackToGPS = true,

  /// If set to true, the app will ignore the last known position
  /// and request a fresh one
  bool ignoreLastKnownPosition = true,

  /// The duration of the location request.
  /// Only valid on Android.
  double? expirationDuration,

  /// The expiration time of the location request.
  /// Only valid on Android.
  double? expirationTime,

  /// The fastest interval between location updates.
  /// In milliseconds.
  /// Only valid on Android.
  double fastestInterval = 500,

  /// The interval between location updates.
  /// In milliseconds.
  double interval = 1000,

  /// The maximum amount of time the app will wait for a location.
  /// In milliseconds.
  double? maxWaitTime,

  /// The number of location updates to request.
  /// Only valid on Android.
  int? numUpdates,

  /// The accuracy of the location request.
  LocationAccuracy accuracy = LocationAccuracy.high,

  /// The smallest displacement between location updates.
  double smallestDisplacement = 0,

  /// If set to true, the app will wait for an accurate location.
  /// Only valid on Android.
  bool waitForAccurateLocation = true,

  /// The acceptable accuracy of the location request.
  /// Only valid on Android.
  double? acceptableAccuracy,
}) async {
  final response = await _platform.setLocationSettings(
    LocationSettings(
      askForPermission: askForPermission,
      rationaleMessageForPermissionRequest:
          rationaleMessageForPermissionRequest,
      rationaleMessageForGPSRequest: rationaleMessageForGPSRequest,
      useGooglePlayServices: useGooglePlayServices,
      askForGooglePlayServices: askForGooglePlayServices,
      askForGPS: askForGPS,
      fallbackToGPS: fallbackToGPS,
      ignoreLastKnownPosition: ignoreLastKnownPosition,
      expirationDuration: expirationDuration,
      expirationTime: expirationTime,
      fastestInterval: fastestInterval,
      interval: interval,
      maxWaitTime: maxWaitTime,
      numUpdates: numUpdates,
      accuracy: accuracy,
      smallestDisplacement: smallestDisplacement,
      waitForAccurateLocation: waitForAccurateLocation,
      acceptableAccuracy: acceptableAccuracy,
    ),
  );
  if (response != true) throw Exception('Unable to set new location settings');
}