Skip to content

[Question]: Background location has gaps - how to configure? #1727

@rossrossp

Description

@rossrossp

Please check the following before submitting a new issue.

Please select for which platform(s) you need help

  • Android
  • iOS
  • Linux
  • macOS
  • Web
  • Windows

Your question

Hello everyone,

I have set up background location tracking by following the instructions, but I am noticing some gaps between positions that I would not expect (see below screenshots).

Image
Image

This is with "Always" permissions enabled, and battery optimisation set to unrestricted for the app.

I believe I see more gaps within journeys by car/train, but I have seen one gap whilst the app is stationary.

Here are some relevant settings:

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET"/>

tracking_page.dart:

void _startStopTracking() {
    _positionSubscription?.cancel();
    if (!_isTracking) {
      _positionSubscription = getPositionStream().listen((Position position) {
        _addPosition(position);
      });
    }
   
    setState(() {
      _isTracking = !_isTracking;
    });
  }

Stream<Position> getPositionStream() {
  late LocationSettings locationSettings = LocationSettings(
    accuracy: LocationAccuracy.high,
    distanceFilter: 1,
  );

  if (Platform.isAndroid) {
    locationSettings = AndroidSettings(
      accuracy: LocationAccuracy.high,
      distanceFilter: 1,
      foregroundNotificationConfig: const ForegroundNotificationConfig(
        notificationText:
            "The app will continue to receive your location even when you aren't using it",
        notificationTitle: "Running in Background",
        enableWakeLock: true,
      ),
    );
  }
  return Geolocator.getPositionStream(locationSettings: locationSettings);
}

The foreground notification is showing, as seen here:
Image

Having said that, I have seen that just because the notification is showing it doesn't mean that a foreground service is running - is geolocator explicitly running a foreground service?

In general, can you see anything obvious here that would cause there to be gaps in positional data whilst the app is in the background? Or anything you'd suggest?

Version

14.0.0

Metadata

Metadata

Assignees

Labels

status: needs more infoWe need more information before we can continue work on this issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions