onLocation static method

void onLocation(
  1. dynamic success(
    1. Location
    ), [
  2. dynamic failure(
    1. LocationError
    )?
])

Subscribe to location events

NOTE: When performing a onMotionChange or getCurrentPosition, the plugin requests multiple location samples in order to record the most accurate location possible. These samples are not persisted to the database but they will be provided to your callback, for your convenience, since it can take some seconds for the best possible location to arrive.

For example, you might use these samples to progressively update the user's position on a map. You can detect these samples in your callback via location.sample == true. If you're manually POSTing location to your server, you should ignore these locations.

Example

BackgroundGeolocation.onLocation((Location location) {
  print('[onLocation] success: ${location}');
}, (LocationError error) {
  print('[onLocation] ERROR: ${error}');
});

Error Codes

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

Implementation

static void onLocation(Function(Location) success,
    [Function(LocationError)? failure]) {
  if (_eventsLocation == null) {
    _eventsLocation = _eventChannelLocation
        .receiveBroadcastStream()
        .map((dynamic event) => Location(event));
  }
  _registerSubscription(
      _eventsLocation!.listen(success, onError: (dynamic error) {
        if (failure != null) {
          failure(LocationError(error));
        } else {
          _onLocationError(LocationError(error));
        }
      }),
      success);
}