Baseflow / flutter-geolocator

Android and iOS Geolocation plugin for Flutter
https://baseflow.com/
MIT License
1.24k stars 657 forks source link

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

Open orkun1675 opened 2 months ago

orkun1675 commented 2 months ago

Please check the following before submitting a new issue.

Please select for which platform(s) you need help

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 commented 3 weeks ago

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 commented 3 weeks ago

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.

thorito commented 1 day ago

When is it expected to be implemented?

orkun1675 commented 1 day ago

When is it expected to be implemented?

Please see my fork at: https://github.com/orkun1675/flutter-geolocator. You can point your pubspec to it to use it.

If Baseflow folks are interested and reply to this bug, I'll clean up the code and open a PR.