Skip to content

Allow updating location stream settings #1664

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions geolocator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 13.1.0

- Adds updatePositionStream API for changing parameters while a position stream is running.

## 13.0.2

- Updates dependency on geolocator_apple to version 2.3.8.
Expand Down
5 changes: 5 additions & 0 deletions geolocator/example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

<!-- Permissions options for the `location` group -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />

<application
android:name="${applicationName}"
Expand Down
46 changes: 23 additions & 23 deletions geolocator/example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>App</string>
<key>CFBundleIdentifier</key>
<string>io.flutter.flutter.app</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>App</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>11.0</string>
</dict>
</plist>
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>App</string>
<key>CFBundleIdentifier</key>
<string>io.flutter.flutter.app</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>App</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>12.0</string>
</dict>
</plist>
31 changes: 26 additions & 5 deletions geolocator/example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -149,6 +149,7 @@
97C146EC1CF9000F007C117D /* Resources */,
9705A1C41CF9048500538489 /* Embed Frameworks */,
3B06AD1E1E4923F5004D2608 /* Thin Binary */,
2169DAB2B55B50BFEA564873 /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand All @@ -165,7 +166,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1020;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down Expand Up @@ -206,12 +207,31 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
2169DAB2B55B50BFEA564873 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources-${CONFIGURATION}-input-files.xcfilelist",
);
name = "[CP] Copy Pods Resources";
outputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources-${CONFIGURATION}-output-files.xcfilelist",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources.sh\"\n";
showEnvVarsInLog = 0;
};
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
);
name = "Thin Binary";
outputPaths = (
Expand Down Expand Up @@ -244,6 +264,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -332,7 +353,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down Expand Up @@ -415,7 +436,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -464,7 +485,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1020"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
4 changes: 4 additions & 0 deletions geolocator/example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
71 changes: 68 additions & 3 deletions geolocator/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'dart:async';
import 'dart:io' show Platform;

import 'package:baseflow_plugin_template/baseflow_plugin_template.dart';
import 'package:flutter/material.dart';

import 'package:baseflow_plugin_template/baseflow_plugin_template.dart';
import 'package:geolocator/geolocator.dart';

/// Defines the main theme color.
Expand Down Expand Up @@ -39,9 +40,11 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {

final GeolocatorPlatform _geolocatorPlatform = GeolocatorPlatform.instance;
final List<_PositionItem> _positionItems = <_PositionItem>[];
final ScrollController _scrollController = ScrollController();
StreamSubscription<Position>? _positionStreamSubscription;
StreamSubscription<ServiceStatus>? _serviceStatusStreamSubscription;
bool positionStreamStarted = false;
bool fastUpdates = false;

@override
void initState() {
Expand Down Expand Up @@ -120,6 +123,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
(context) => Scaffold(
backgroundColor: Theme.of(context).colorScheme.surface,
body: ListView.builder(
controller: _scrollController,
itemCount: _positionItems.length,
itemBuilder: (context, index) {
final positionItem = _positionItems[index];
Expand Down Expand Up @@ -167,6 +171,17 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
: const Icon(Icons.pause),
),
sizedBox,
if (_positionStreamSubscription != null)
Column(
mainAxisSize: MainAxisSize.min,
children: [
FloatingActionButton(
onPressed: _toggleUpdateSpeed,
child: const Icon(Icons.speed),
),
sizedBox,
],
),
FloatingActionButton(
onPressed: _getCurrentPosition,
child: const Icon(Icons.my_location),
Expand Down Expand Up @@ -252,9 +267,12 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
return true;
}

void _updatePositionList(_PositionItemType type, String displayValue) {
void _updatePositionList(_PositionItemType type, String displayValue) async {
_positionItems.add(_PositionItem(type, displayValue));
setState(() {});
await Future.delayed(const Duration(milliseconds: 50));
if (!mounted) return;
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
}

bool _isListening() => !(_positionStreamSubscription == null ||
Expand Down Expand Up @@ -299,7 +317,19 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {

void _toggleListening() {
if (_positionStreamSubscription == null) {
final positionStream = _geolocatorPlatform.getPositionStream();
final settings = AndroidSettings(
distanceFilter: 50,
foregroundNotificationConfig: const ForegroundNotificationConfig(
notificationTitle: 'Geolocator is tracking your trip',
notificationText:
'This is a persistent notification and is used to start a foreground service.',
enableWifiLock: true,
enableWakeLock: true,
setOngoing: true,
),
);
final positionStream =
_geolocatorPlatform.getPositionStream(locationSettings: settings);
_positionStreamSubscription = positionStream.handleError((error) {
_positionStreamSubscription?.cancel();
_positionStreamSubscription = null;
Expand Down Expand Up @@ -331,6 +361,41 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
});
}

Future<void> _toggleUpdateSpeed() async {
fastUpdates = !fastUpdates;

final LocationSettings settings;
if (Platform.isAndroid) {
settings = AndroidSettings(
accuracy: LocationAccuracy.best,
distanceFilter: 0,
intervalDuration: Duration(seconds: fastUpdates ? 1 : 10),
forceLocationManager: false,
useMSLAltitude: true,
);
} else if (Platform.isIOS) {
settings = AppleSettings(
accuracy: LocationAccuracy.best,
distanceFilter: fastUpdates ? 0 : 50,
activityType: ActivityType.fitness,
showBackgroundLocationIndicator: false,
allowBackgroundLocationUpdates: false,
);
} else {
settings = LocationSettings(
accuracy: LocationAccuracy.best,
distanceFilter: fastUpdates ? 0 : 50,
);
}

await _geolocatorPlatform.updatePositionStream(locationSettings: settings);

_updatePositionList(
_PositionItemType.log,
'Position updates ${fastUpdates ? 'sped up' : 'slowed down'}.',
);
}

@override
void dispose() {
if (_positionStreamSubscription != null) {
Expand Down
8 changes: 4 additions & 4 deletions geolocator/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
sdk: ">=2.15.0 <3.0.0"

dependencies:
baseflow_plugin_template: ^2.1.2
baseflow_plugin_template: ^2.2.0

flutter:
sdk: flutter
Expand All @@ -24,16 +24,16 @@ dependencies:

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.1+1
cupertino_icons: ^1.0.8

# The following adds the URL Launcher plugin which is used by
# the demo application to open the links in the web browser.
url_launcher: ^6.0.0-nullsafety.1
url_launcher: ^6.3.1

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^3.0.1
flutter_lints: ^5.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down
12 changes: 12 additions & 0 deletions geolocator/lib/geolocator.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flutter/foundation.dart';

import 'package:geolocator_android/geolocator_android.dart';
import 'package:geolocator_apple/geolocator_apple.dart';
import 'package:geolocator_platform_interface/geolocator_platform_interface.dart';
Expand Down Expand Up @@ -192,6 +193,17 @@ class Geolocator {
locationSettings: locationSettings,
);

/// Updates the parameters of the active position stream.
///
/// Throws a [NotSubscribedException] when there is no active position stream
/// to update.
static Future<void> updatePositionStream({
required LocationSettings locationSettings,
}) =>
GeolocatorPlatform.instance.updatePositionStream(
locationSettings: locationSettings,
);

/// Returns a [Future] containing a [LocationAccuracyStatus]
/// When the user has given permission for approximate location,
/// [LocationAccuracyStatus.reduced] will be returned, if the user has
Expand Down
21 changes: 13 additions & 8 deletions geolocator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: geolocator
description: Geolocation plugin for Flutter. This plugin provides a cross-platform (iOS, Android) API for generic location (GPS etc.) functions.
repository: https://github.com/baseflow/flutter-geolocator/tree/main/geolocator
issue_tracker: https://github.com/baseflow/flutter-geolocator/issues?q=is%3Aissue+is%3Aopen
version: 13.0.2
version: 13.1.0

environment:
sdk: ">=2.15.0 <4.0.0"
Expand All @@ -26,15 +26,20 @@ dependencies:
flutter:
sdk: flutter

geolocator_platform_interface: ^4.2.3
geolocator_android: ^4.6.0
geolocator_apple: ^2.3.8
geolocator_web: ^4.1.1
geolocator_windows: ^0.2.3
geolocator_platform_interface:
path: ../geolocator_platform_interface
geolocator_android:
path: ../geolocator_android
geolocator_apple:
path: ../geolocator_apple
geolocator_web:
path: ../geolocator_web
geolocator_windows:
path: ../geolocator_windows

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ">=3.0.1 <5.0.0"
mockito: ^5.0.0-nullsafety.7
flutter_lints: ">=5.0.0"
mockito: ^5.4.4
plugin_platform_interface: ^2.1.8
Loading