Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: Updating accuracy/distanceFilter while the Position stream is running #1569

Open
4 of 8 tasks
orkun1675 opened this issue Sep 2, 2024 · 2 comments
Open
4 of 8 tasks

Comments

@orkun1675
Copy link

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

First of all, thank you for the great plugin!

I'm building a navigation app that needs low frequency (high distanceFilter and intervalDuration) location updates at the beginning but higher frequency (low distanceFilter and intervalDuration) as the user reaches the end of their journey.

IIUC iOS recommends adjusting the desiredAccuracy and distancefilter values to optimize for battery usage. It makes sense that this applies to Android as well.

Is there a way to update these values without cancelling and re-creating the Position stream?

Or is there a way to retain the Foreground service even when cancelling and re-creating the stream?
Because otherwise on Android re-creating fails with android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false; as applications running in the background cannot create foreground services.

I'm happy to contribute a PR if this feature does not exist and you'd be willing to point me in the right direction.

Version

12.0.0

@orkun1675
Copy link
Author

orkun1675 commented Oct 19, 2024

I've attempted to implement this by adding a new function with the following signature:

static Future<void> updatePositionStream({
    required LocationSettings locationSettings,
  });

Full source code: https://github.com/orkun1675/flutter-geolocator

Would this approach be something Baseflow is intrested in to contribute back to main?

@orkun1675
Copy link
Author

orkun1675 commented Oct 19, 2024

This approach is possible because:

  1. iOS CLLocationManager allows changing distanceFilter & accuracy values while a listener is running.

[needs source]

  1. Android FusedLocationProviderClient will replace the existing listener LocationRequest settings when requestLocationUpdates() is called again with the same listener.

A previous request for location updates for the same listener will be replaced by this request.

source

  1. Android LocationManagerCompat will replace the LocationRequestCompat settings if requestLocationUpdates is called with the same provider & listener.

Only one request can be registered for each unique listener/provider pair, so any subsequent requests with the same provider and listener will overwrite all associated arguments.

source


I have tested these on emulators and they are WAI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant