getCurrentPosition method
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,
String? requestId,
}) async {
requestId = requestId ?? _uuid.v4();
try {
Future<dynamic> positionFuture;
final Duration? timeLimit = locationSettings?.timeLimit;
positionFuture = _methodChannel.invokeMethod(
'getCurrentPosition',
{
...?locationSettings?.toJson(),
'requestId': requestId,
},
);
if (timeLimit != null) {
positionFuture = positionFuture.timeout(timeLimit);
}
final positionMap = await positionFuture;
return AndroidPosition.fromMap(positionMap);
} on TimeoutException {
final parameters = <String, dynamic>{
'requestId': requestId,
};
_methodChannel.invokeMethod(
'cancelGetCurrentPosition',
parameters,
);
rethrow;
} on PlatformException catch (e) {
final error = _handlePlatformException(e);
throw error;
}
}