getCurrentPosition method

  1. @override
Future<Position> getCurrentPosition({
  1. LocationSettings? locationSettings,
})

Returns the current position.

You can control the settings used for retrieving the location by supplying locationSettings.

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.

Note: On Android, when setting the location accuracy, 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

@override
Future<Position> getCurrentPosition({
  LocationSettings? locationSettings,
}) async {
  try {
    Future<dynamic> positionFuture;

    var timeLimit = locationSettings?.timeLimit;

    if (timeLimit != null) {
      positionFuture = _methodChannel
          .invokeMethod(
            'getCurrentPosition',
            locationSettings?.toJson(),
          )
          .timeout(timeLimit);
    } else {
      positionFuture = _methodChannel.invokeMethod(
        'getCurrentPosition',
        locationSettings?.toJson(),
      );
    }

    final positionMap = await positionFuture;
    return Position.fromMap(positionMap);
  } on PlatformException catch (e) {
    final error = _handlePlatformException(e);

    throw error;
  }
}