getCurrentPosition static method

Future<Position> getCurrentPosition({
  1. LocationAccuracy desiredAccuracy = LocationAccuracy.best,
  2. bool forceAndroidLocationManager = false,
  3. Duration? timeLimit,
})

Returns the current position.

You can control the precision of the location updates by supplying the desiredAccuracy parameter (defaults to "best"). On Android you can force the use of the Android LocationManager instead of the FusedLocationProvider by setting the forceAndroidLocationManager parameter to true. The timeLimit parameter allows you to specify a timeout interval (by default no time limit is configured).

Calling the getCurrentPosition method will request the platform to obtain a location fix. Depending on the availability of different location services, this can take several seconds. The recommended use would be to call the getLastKnownPosition method to receive a cached position and update it with the result of the getCurrentPosition method.

Throws a TimeoutException when no location is received within the supplied timeLimit duration. Throws a LocationServiceDisabledException when the user allowed access, but the location services of the device are disabled.

Note: On Android the location accuracy is interpreted as location priority. The interpretation works as follows:

LocationAccuracy.lowest -> PRIORITY_PASSIVE: Ensures that no extra power will be used to derive locations. This enforces that the request will act as a passive listener that will only receive "free" locations calculated on behalf of other clients, and no locations will be calculated on behalf of only this request.

LocationAccuracy.low -> PRIORITY_LOW_POWER: Requests a tradeoff that favors low power usage at the possible expense of location accuracy.

LocationAccuracy.medium -> PRIORITY_BALANCED_POWER_ACCURACY: Requests a tradeoff that is balanced between location accuracy and power usage.

LocationAccuracy.high+ -> PRIORITY_HIGH_ACCURACY: Requests a tradeoff that favors highly accurate locations at the possible expense of additional power usage.

Implementation

static Future<Position> getCurrentPosition({
  LocationAccuracy desiredAccuracy = LocationAccuracy.best,
  bool forceAndroidLocationManager = false,
  Duration? timeLimit,
}) {
  late LocationSettings locationSettings;
  if (defaultTargetPlatform == TargetPlatform.android) {
    locationSettings = AndroidSettings(
      accuracy: desiredAccuracy,
      forceLocationManager: forceAndroidLocationManager,
      timeLimit: timeLimit,
    );
  } else {
    locationSettings = LocationSettings(
      accuracy: desiredAccuracy,
      timeLimit: timeLimit,
    );
  }

  return GeolocatorPlatform.instance
      .getCurrentPosition(locationSettings: locationSettings);
}