getCurrentPosition static method

Future<Location> getCurrentPosition({
  1. int? samples,
  2. int? timeout,
  3. int? maximumAge,
  4. bool? persist,
  5. int? desiredAccuracy,
  6. Map<String, dynamic>? extras,
})

Retrieves the current Location.

This method instructs the native code to fetch exactly one location using maximum power & accuracy. The native code will persist the fetched location to its SQLite database just as any other location in addition to POSTing to your configured Config.url. If an error occurs while fetching the location, catchError will be provided with an Integer Error Code.

Options

@config {int seconds} timeout [30] An optional location-timeout. If the timeout expires before a Location is retrieved, an error will fire..

@config {int millis} maximumAge [0] Accept the last-recorded-location if no older than supplied value in milliseconds.

@config {bool} persist [true] Defaults to true. Set false to disable persisting the retrieved Location in the plugin's SQLite database.

@config {int} samples [3] Sets the maximum number of location-samples to fetch. The plugin will return the Location having the best accuracy. Defaults to 3. Only the final Location will be persisted.

@config {int} desiredAccuracy [Config.stationaryRadius] Sets the desired accuracy of location you're attempting to fetch. When a location having accuracy <= desiredAccuracy is retrieved, the plugin will stop sampling and immediately return that location. Defaults to your configured Config.stationaryRadius.

@config {Map} extras Optional extra-data to attach to the location. These extras will be merged to the configure Config.extras and persisted / POSTed to your server (if you've configured the HTTP Layer).

Error Codes

Code Error
0 Location unknown
1 Location permission denied
2 Network error
408 Location timeout

Example

Location location = await BackgroundGeolocation.getCurrentPosition(
  timeout: 30,          // 30 second timeout to fetch location
  maximumAge: 5000,     // Accept the last-known-location if not older than 5000 ms.
  desiredAccuracy: 10,  // Try to fetch a location with an accuracy of `10` meters.
  samples: 3,           // How many location samples to attempt.
  extras: {             // [Optional] Attach your own custom meta-data to this location.  This meta-data will be persisted to SQLite and POSTed to your server
    "foo": "bar"
  }
});

NOTE: While getCurrentPosition will receive only one Location, the plugin does request multiple location samples which will all be provided to the onLocation event-listener. You can detect these samples via Location.sample == true.

Implementation

static Future<Location> getCurrentPosition(
    {int? samples,
    int? timeout,
    int? maximumAge,
    bool? persist,
    int? desiredAccuracy,
    Map<String, dynamic>? extras}) async {
  Map<String, dynamic> options = {};
  if (samples != null) options['samples'] = samples;
  if (timeout != null) options['timeout'] = timeout;
  if (maximumAge != null) options['maximumAge'] = maximumAge;
  if (persist != null) options['persist'] = persist;
  if (desiredAccuracy != null) options['desiredAccuracy'] = desiredAccuracy;
  if (extras != null) options['extras'] = extras;

  Completer completer = Completer<Location>();

  _methodChannel
      .invokeMapMethod('getCurrentPosition', options)
      .then((Map? data) {
    completer.complete(Location(data!));
  }).catchError((error) {
    completer.completeError(LocationError(error));
  });
  return completer.future as FutureOr<Location>;
}