Skip to content

Commit 881786e

Browse files
adds WebSettings to allow controlling maximumAge of location update (#1526)
* adds WebSettings to allow controlling maximumAge of location update * Update CHANGELOG.md * Update pubspec.yaml * Update CHANGELOG.md * Update pubspec.yaml * Update CHANGELOG.md * Update pubspec.yaml --------- Co-authored-by: Maurits van Beusekom <maurits@baseflow.com>
1 parent b741647 commit 881786e

14 files changed

+161
-44
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ We really appreciate contributions via GitHub pull requests. To contribute take
3636
* `git checkout upstream/develop -b <name_of_your_branch>`
3737
* Apply your changes
3838
* Verify your changes and fix potential warnings/ errors:
39-
* Check formatting: `flutter format .`
39+
* Check formatting: `dart format .`
4040
* Run static analyses: `flutter analyze`
4141
* Run unit-tests: `flutter test`
4242
* Commit your changes: `git commit -am "<your informative commit message>"`
@@ -46,4 +46,4 @@ Send us your pull request:
4646

4747
* Go to `https://github.com/BaseflowIT/flutter-geolocator` and click the "Compare & pull request" button.
4848

49-
Please make sure you solved all warnings and errors reported by the static code analyses and that you fill in the full pull request template. Failing to do so will result in us asking you to fix it.
49+
Please make sure you solved all warnings and errors reported by the static code analyses and that you fill in the full pull request template. Failing to do so will result in us asking you to fix it.

geolocator/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 13.0.0
2+
3+
- **BREAKING CHANGE:** Deprecates getCurrentPosition desiredAccuracy, forceAndroidLocationManager, and timeLimit parameters in favor of supplying a LocationSettings class.
4+
- Exposes `WebSettings` from geolocator.
5+
- Updates dependency on geolocator_web to version 4.1.0
6+
- Updates dependency on geolocator_android to version 4.3.0
7+
- Updates dependency on geolocator_apple to version 2.3.7
8+
- Updates dependency on geolocator_windows to version 0.2.3
9+
- Updates dependency on geolocator_platform_interface to version 4.2.3
10+
111
## 12.0.0
212

313
- **BREAKING CHANGE:** Updates dependency on geolocator_web to version [4.0.0](https://pub.dev/packages/geolocator_web/changelog).

geolocator/README.md

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,12 @@ To query the current location of the device simply make a call to the `getCurren
233233
``` dart
234234
import 'package:geolocator/geolocator.dart';
235235
236-
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
236+
final LocationSettings locationSettings = LocationSettings(
237+
accuracy: LocationAccuracy.high,
238+
distanceFilter: 100,
239+
);
240+
241+
Position position = await Geolocator.getCurrentPosition(locationSettings: locationSettings);
237242
```
238243

239244
#### Last known location
@@ -267,29 +272,32 @@ StreamSubscription<Position> positionStream = Geolocator.getPositionStream(locat
267272
});
268273
```
269274

270-
In certain situation it is necessary to specify some platform specific settings. This can be accomplished using the platform specific `AndroidSettings` or `AppleSettings` classes. When using a platform specific class, the platform specific package must be imported as well. For example:
275+
### Platform specific location settings
276+
277+
In certain situation it is necessary to specify some platform specific settings. This can be accomplished using the platform specific `AndroidSettings`, `AppleSettings` and `WebSettings` classes. When using a platform specific class, the platform specific package must be imported as well. For example:
271278

272279
```dart
273280
import 'package:geolocator/geolocator.dart';
274-
import 'package:geolocator_apple/geolocator_apple.dart';
275281
import 'package:geolocator_android/geolocator_android.dart';
282+
import 'package:geolocator_android/geolocator_web.dart';
283+
import 'package:geolocator_apple/geolocator_apple.dart';
276284
277285
late LocationSettings locationSettings;
278286
279287
if (defaultTargetPlatform == TargetPlatform.android) {
280288
locationSettings = AndroidSettings(
281-
accuracy: LocationAccuracy.high,
282-
distanceFilter: 100,
283-
forceLocationManager: true,
284-
intervalDuration: const Duration(seconds: 10),
285-
//(Optional) Set foreground notification config to keep the app alive
286-
//when going to the background
287-
foregroundNotificationConfig: const ForegroundNotificationConfig(
289+
accuracy: LocationAccuracy.high,
290+
distanceFilter: 100,
291+
forceLocationManager: true,
292+
intervalDuration: const Duration(seconds: 10),
293+
//(Optional) Set foreground notification config to keep the app alive
294+
//when going to the background
295+
foregroundNotificationConfig: const ForegroundNotificationConfig(
288296
notificationText:
289297
"Example app will continue to receive your location even when you aren't using it",
290298
notificationTitle: "Running in Background",
291299
enableWakeLock: true,
292-
)
300+
)
293301
);
294302
} else if (defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.macOS) {
295303
locationSettings = AppleSettings(
@@ -300,16 +308,26 @@ if (defaultTargetPlatform == TargetPlatform.android) {
300308
// Only set to true if our app will be started up in the background.
301309
showBackgroundLocationIndicator: false,
302310
);
311+
} else if (kIsWeb) {
312+
locationSettings = WebSettings(
313+
accuracy: LocationAccuracy.high,
314+
distanceFilter: 100,
315+
maximumAge: Duration(minutes: 5),
316+
);
303317
} else {
304-
locationSettings = LocationSettings(
318+
locationSettings = LocationSettings(
305319
accuracy: LocationAccuracy.high,
306320
distanceFilter: 100,
307321
);
308322
}
309323
324+
// supply location settings to getCurrentPosition
325+
Position position = await Geolocator.getCurrentPosition(locationSettings: locationSettings);
326+
327+
// supply location settings to getPositionStream
310328
StreamSubscription<Position> positionStream = Geolocator.getPositionStream(locationSettings: locationSettings).listen(
311-
(Position? position) {
312-
print(position == null ? 'Unknown' : '${position.latitude.toString()}, ${position.longitude.toString()}');
329+
(Position? position) {
330+
print(position == null ? 'Unknown' : '${position.latitude.toString()}, ${position.longitude.toString()}');
313331
});
314332
```
315333

geolocator/lib/geolocator.dart

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export 'package:geolocator_android/geolocator_android.dart'
1313
AndroidPosition;
1414
export 'package:geolocator_apple/geolocator_apple.dart'
1515
show AppleSettings, ActivityType;
16+
export 'package:geolocator_apple/geolocator_web.dart' show WebSettings;
1617
export 'package:geolocator_platform_interface/geolocator_platform_interface.dart';
1718

1819
/// Wraps CLLocationManager (on iOS) and FusedLocationProviderClient or
@@ -60,6 +61,21 @@ class Geolocator {
6061

6162
/// Returns the current position.
6263
///
64+
/// You can control the behavior of the location update by specifying an instance of
65+
/// the [LocationSettings] class for the [locationSettings] parameter.
66+
/// Standard settings are:
67+
/// * `LocationSettings.accuracy`: allows controlling the precision of the position updates by
68+
/// supplying (defaults to "best");
69+
/// * `LocationSettings.distanceFilter`: allows controlling the minimum
70+
/// distance the device needs to move before the update is emitted (default
71+
/// value is 0 which indicates no filter is used);
72+
/// * `LocationSettings.timeLimit`: allows for setting a timeout interval. If
73+
/// between fetching locations the timeout interval is exceeded a
74+
/// [TimeoutException] will be thrown. By default no time limit is configured.
75+
///
76+
/// If you want to specify platform specific settings you can use the
77+
/// [AndroidSettings], [AppleSettings] and [WebSettings] classes.
78+
///
6379
/// You can control the precision of the location updates by supplying the
6480
/// [desiredAccuracy] parameter (defaults to "best").
6581
/// On Android you can force the use of the Android LocationManager instead of
@@ -101,26 +117,36 @@ class Geolocator {
101117
/// Requests a tradeoff that favors highly accurate locations at the possible
102118
/// expense of additional power usage.
103119
static Future<Position> getCurrentPosition({
120+
LocationSettings? locationSettings,
121+
@Deprecated(
122+
"use settings parameter with AndroidSettings, AppleSettings, WebSettings, or LocationSettings")
104123
LocationAccuracy desiredAccuracy = LocationAccuracy.best,
124+
@Deprecated(
125+
"use settings parameter with AndroidSettings, AppleSettings, WebSettings, or LocationSettings")
105126
bool forceAndroidLocationManager = false,
127+
@Deprecated(
128+
"use settings parameter with AndroidSettings, AppleSettings, WebSettings, or LocationSettings")
106129
Duration? timeLimit,
107130
}) {
108-
late LocationSettings locationSettings;
109-
if (defaultTargetPlatform == TargetPlatform.android) {
110-
locationSettings = AndroidSettings(
131+
LocationSettings? settings;
132+
133+
if (locationSettings != null) {
134+
settings = locationSettings;
135+
} else if (defaultTargetPlatform == TargetPlatform.android) {
136+
settings = AndroidSettings(
111137
accuracy: desiredAccuracy,
112138
forceLocationManager: forceAndroidLocationManager,
113139
timeLimit: timeLimit,
114140
);
115-
} else {
116-
locationSettings = LocationSettings(
117-
accuracy: desiredAccuracy,
118-
timeLimit: timeLimit,
119-
);
120141
}
121142

143+
settings ??= LocationSettings(
144+
accuracy: desiredAccuracy,
145+
timeLimit: timeLimit,
146+
);
147+
122148
return GeolocatorPlatform.instance
123-
.getCurrentPosition(locationSettings: locationSettings);
149+
.getCurrentPosition(locationSettings: settings);
124150
}
125151

126152
/// Fires whenever the location changes inside the bounds of the
@@ -153,7 +179,7 @@ class Geolocator {
153179
/// [TimeoutException] will be thrown. By default no time limit is configured.
154180
///
155181
/// If you want to specify platform specific settings you can use the
156-
/// [AndroidSettings] and [AppleSettings] classes.
182+
/// [AndroidSettings], [AppleSettings] and [WebSettings] classes.
157183
///
158184
/// Throws a [TimeoutException] when no location is received within the
159185
/// supplied [timeLimit] duration.

geolocator/pubspec.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: geolocator
22
description: Geolocation plugin for Flutter. This plugin provides a cross-platform (iOS, Android) API for generic location (GPS etc.) functions.
33
repository: https://github.com/baseflow/flutter-geolocator/tree/main/geolocator
44
issue_tracker: https://github.com/baseflow/flutter-geolocator/issues?q=is%3Aissue+is%3Aopen
5-
version: 12.0.0
5+
version: 13.0.0
66

77
environment:
88
sdk: ">=2.15.0 <4.0.0"
@@ -26,15 +26,15 @@ dependencies:
2626
flutter:
2727
sdk: flutter
2828

29-
geolocator_platform_interface: ^4.1.0
30-
geolocator_android: ^4.3.0
31-
geolocator_apple: ^2.3.0
32-
geolocator_web: ^4.0.0
33-
geolocator_windows: ^0.2.2
29+
geolocator_platform_interface: ^4.2.3
30+
geolocator_android: ^4.6.0
31+
geolocator_apple: ^2.3.7
32+
geolocator_web: ^4.1.0
33+
geolocator_windows: ^0.2.3
3434

3535
dev_dependencies:
3636
flutter_test:
3737
sdk: flutter
3838
flutter_lints: ">=3.0.1 <5.0.0"
3939
mockito: ^5.0.0-nullsafety.7
40-
plugin_platform_interface: ^2.0.0
40+
plugin_platform_interface: ^2.1.8

geolocator_web/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
## 4.1.0
2+
3+
- Adds WebSettings class, allowing a location maximumAge to be specified.
4+
- Supports maximumAge parameter in `getCurrentPosition` and `watchPosition` methods.
5+
16
## 4.0.1
27

38
- Upgrades the package:web dependency to version 1.0.0.
4-
- Upgrades Dart SDK from 3.3.0 to 3.4.0 for geolocator_web
9+
- Upgrades Dart SDK from 3.3.0 to 3.4.0 for geolocator_web.
510

611
## 4.0.0
712

geolocator_web/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Please file any issues, bugs or feature requests as an issue on our [GitHub](htt
1818

1919
If you would like to contribute to the plugin (e.g. by improving the documentation, solving a bug or adding a cool new feature), please carefully review our [contribution guide](../CONTRIBUTING.md) and send us your [pull request](https://github.com/Baseflow/flutter-geolocator/pulls).
2020

21+
## Tests
22+
23+
Tests require being run on a browser due to the use of the `dart:js_interop` package:
24+
25+
`flutter test --platform chrome`
26+
2127
## Author
2228

23-
This Geolocator plugin for Flutter is developed by [Baseflow](https://baseflow.com).
29+
This Geolocator plugin for Flutter is developed by [Baseflow](https://baseflow.com).

geolocator_web/lib/geolocator_web.dart

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import 'src/geolocation_manager.dart';
77
import 'src/permissions_manager.dart';
88
import 'src/html_geolocation_manager.dart';
99
import 'src/html_permissions_manager.dart';
10+
import 'src/types/web_settings.dart';
1011

1112
export 'package:geolocator_platform_interface/geolocator_platform_interface.dart';
13+
export 'src/types/web_settings.dart' show WebSettings;
1214

1315
/// The web implementation of [GeolocatorPlatform].
1416
///
@@ -73,9 +75,11 @@ class GeolocatorPlugin extends GeolocatorPlatform {
7375
LocationSettings? locationSettings,
7476
}) async {
7577
final result = await _geolocation.getCurrentPosition(
76-
enableHighAccuracy: _enableHighAccuracy(locationSettings?.accuracy),
77-
timeout: locationSettings?.timeLimit,
78-
);
78+
enableHighAccuracy: _enableHighAccuracy(locationSettings?.accuracy),
79+
timeout: locationSettings?.timeLimit,
80+
maximumAge: locationSettings is WebSettings
81+
? locationSettings.maximumAge
82+
: null);
7983

8084
return result;
8185
}
@@ -88,9 +92,11 @@ class GeolocatorPlugin extends GeolocatorPlatform {
8892

8993
return _geolocation
9094
.watchPosition(
91-
enableHighAccuracy: _enableHighAccuracy(locationSettings?.accuracy),
92-
timeout: locationSettings?.timeLimit,
93-
)
95+
enableHighAccuracy: _enableHighAccuracy(locationSettings?.accuracy),
96+
timeout: locationSettings?.timeLimit,
97+
maximumAge: locationSettings is WebSettings
98+
? locationSettings.maximumAge
99+
: null)
94100
.skipWhile((geoposition) {
95101
if (locationSettings?.distanceFilter == 0 ||
96102
locationSettings?.distanceFilter == null) {

geolocator_web/lib/src/geolocation_manager.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ abstract class GeolocationManager {
99
/// possible position fix. This can result in slower response times and higher
1010
/// battery consumption.
1111
///
12+
/// [maximumAge] indicates the maximum age of a possible cached position that is acceptable to return.
13+
/// If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position.
14+
///
1215
/// Throws a [TimeoutException] when no position is received within the
1316
/// supplied [timeout] duration.
1417
Future<Position> getCurrentPosition({
1518
bool? enableHighAccuracy,
1619
Duration? timeout,
20+
Duration? maximumAge,
1721
});
1822

1923
/// Returns a position stream providing continuous position updates.
@@ -22,10 +26,14 @@ abstract class GeolocationManager {
2226
/// possible location fix. This can result in slower response times and higher
2327
/// battery consumption.
2428
///
29+
/// [maximumAge] indicates the maximum age of a possible cached position that is acceptable to return.
30+
/// If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position.
31+
///
2532
/// Throws a [TimeoutException] when no location is received within the
2633
/// supplied [timeout] duration.
2734
Stream<Position> watchPosition({
2835
bool? enableHighAccuracy,
2936
Duration? timeout,
37+
Duration? maximumAge,
3038
});
3139
}

geolocator_web/lib/src/html_geolocation_manager.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class HtmlGeolocationManager implements GeolocationManager {
1919
Future<Position> getCurrentPosition({
2020
bool? enableHighAccuracy,
2121
Duration? timeout,
22+
Duration? maximumAge,
2223
}) async {
2324
Completer<Position> completer = Completer();
2425
try {
@@ -33,6 +34,7 @@ class HtmlGeolocationManager implements GeolocationManager {
3334
enableHighAccuracy: enableHighAccuracy ?? false,
3435
timeout:
3536
timeout?.inMicroseconds ?? const Duration(days: 1).inMilliseconds,
37+
maximumAge: maximumAge?.inMilliseconds ?? 0,
3638
),
3739
);
3840
} catch (e) {
@@ -47,6 +49,7 @@ class HtmlGeolocationManager implements GeolocationManager {
4749
Stream<Position> watchPosition({
4850
bool? enableHighAccuracy,
4951
Duration? timeout,
52+
Duration? maximumAge,
5053
}) {
5154
int? watchId;
5255
StreamController<Position> controller = StreamController<Position>(
@@ -69,6 +72,7 @@ class HtmlGeolocationManager implements GeolocationManager {
6972
enableHighAccuracy: enableHighAccuracy ?? false,
7073
timeout:
7174
timeout?.inMicroseconds ?? const Duration(days: 1).inMilliseconds,
75+
maximumAge: maximumAge?.inMilliseconds ?? 0,
7276
),
7377
);
7478
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'package:geolocator_platform_interface/geolocator_platform_interface.dart';
2+
3+
/// Represents different Web specific settings with which you can set a value
4+
/// other then the default value of the setting.
5+
class WebSettings extends LocationSettings {
6+
/// Initializes a new [WebSpecificSettings] instance with default values.
7+
WebSettings({
8+
super.accuracy,
9+
super.distanceFilter,
10+
this.maximumAge = Duration.zero,
11+
super.timeLimit,
12+
});
13+
14+
/// A value indicating the maximum age of a possible cached position that is acceptable to return.
15+
/// If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position.
16+
/// Default: 0
17+
final Duration maximumAge;
18+
19+
@override
20+
Map<String, dynamic> toJson() {
21+
return super.toJson()
22+
..addAll({
23+
'maximumAge': maximumAge,
24+
});
25+
}
26+
}

0 commit comments

Comments
 (0)