Skip to content

Commit d5c4452

Browse files
feat: add API to enable background location updates on iOS (#61)
Fixes #26 Creates an API to Allow background location updates on iOS
1 parent d830567 commit d5c4452

File tree

9 files changed

+67
-3
lines changed

9 files changed

+67
-3
lines changed

SampleApp/ios/Podfile.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ PODS:
7070
- FlipperKit/FlipperKitNetworkPlugin
7171
- fmt (6.2.1)
7272
- glog (0.3.5)
73+
- GoogleMaps (7.4.0):
74+
- GoogleMaps/Maps (= 7.4.0)
75+
- GoogleMaps/Base (7.4.0)
76+
- GoogleMaps/Maps (7.4.0):
77+
- GoogleMaps/Base
78+
- GoogleNavigation (4.4.0):
79+
- GoogleMaps (= 7.4.0)
7380
- hermes-engine (0.72.6):
7481
- hermes-engine/Pre-built (= 0.72.6)
7582
- hermes-engine/Pre-built (0.72.6)
@@ -375,6 +382,9 @@ PODS:
375382
- React-jsinspector (0.72.6)
376383
- React-logger (0.72.6):
377384
- glog
385+
- react-native-navigation-sdk (0.1.4-beta):
386+
- GoogleNavigation (= 4.4.0)
387+
- React-Core
378388
- React-NativeModulesApple (0.72.6):
379389
- hermes-engine
380390
- React-callinvoker
@@ -542,6 +552,7 @@ DEPENDENCIES:
542552
- React-jsiexecutor (from `../node_modules/react-native/ReactCommon/jsiexecutor`)
543553
- React-jsinspector (from `../node_modules/react-native/ReactCommon/jsinspector`)
544554
- React-logger (from `../node_modules/react-native/ReactCommon/logger`)
555+
- react-native-navigation-sdk (from `../node_modules/react-native-navigation-sdk`)
545556
- React-NativeModulesApple (from `../node_modules/react-native/ReactCommon/react/nativemodule/core/platform/ios`)
546557
- React-perflogger (from `../node_modules/react-native/ReactCommon/reactperflogger`)
547558
- React-RCTActionSheet (from `../node_modules/react-native/Libraries/ActionSheetIOS`)
@@ -576,6 +587,8 @@ SPEC REPOS:
576587
- Flipper-PeerTalk
577588
- FlipperKit
578589
- fmt
590+
- GoogleMaps
591+
- GoogleNavigation
579592
- libevent
580593
- OpenSSL-Universal
581594
- SocketRocket
@@ -625,6 +638,8 @@ EXTERNAL SOURCES:
625638
:path: "../node_modules/react-native/ReactCommon/jsinspector"
626639
React-logger:
627640
:path: "../node_modules/react-native/ReactCommon/logger"
641+
react-native-navigation-sdk:
642+
:path: "../node_modules/react-native-navigation-sdk"
628643
React-NativeModulesApple:
629644
:path: "../node_modules/react-native/ReactCommon/react/nativemodule/core/platform/ios"
630645
React-perflogger:
@@ -684,6 +699,8 @@ SPEC CHECKSUMS:
684699
FlipperKit: 2efad7007d6745a3f95e4034d547be637f89d3f6
685700
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
686701
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
702+
GoogleMaps: 032f676450ba0779bd8ce16840690915f84e57ac
703+
GoogleNavigation: c07206bab1e1d483003a4d95ae58ecfa8d29fd21
687704
hermes-engine: 8057e75cfc1437b178ac86c8654b24e7fead7f60
688705
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
689706
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
@@ -702,6 +719,7 @@ SPEC CHECKSUMS:
702719
React-jsiexecutor: 3bf18ff7cb03cd8dfdce08fbbc0d15058c1d71ae
703720
React-jsinspector: 194e32c6aab382d88713ad3dd0025c5f5c4ee072
704721
React-logger: cebf22b6cf43434e471dc561e5911b40ac01d289
722+
react-native-navigation-sdk: 6b584b850424c800a06d51f055b24b30c6fde91b
705723
React-NativeModulesApple: 02e35e9a51e10c6422f04f5e4076a7c02243fff2
706724
React-perflogger: e3596db7e753f51766bceadc061936ef1472edc3
707725
React-RCTActionSheet: 17ab132c748b4471012abbcdcf5befe860660485

SampleApp/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SampleApp/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"react": "18.2.0",
1515
"react-native": "^0.72.4",
1616
"react-native-eject": "^0.2.0",
17+
"react-native-navigation-sdk": "github:googlemaps/react-native-navigation-sdk",
1718
"react-native-permissions": "^3.10.1",
1819
"react-native-select-dropdown": "^3.4.0",
1920
"react-native-snackbar": "^2.6.2"

SampleApp/src/navigationControls.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ const NavigationControls: React.FC<NavigationControlsProps> = ({
5555
const [speedometerEnabled, setSpeedometerEnabled] = useState(false);
5656
const [trafficIncidentsCardEnabled, setTrafficIncidentsCardEnabled] =
5757
useState(false);
58+
const [backgroundLocationUpdatesEnabled, setBackgroundLocationUpdatesEnabled] =
59+
useState(false);
60+
5861
const [latitude, onLatChanged] = useState('');
5962
const [longitude, onLngChanged] = useState('');
6063

@@ -178,6 +181,12 @@ const NavigationControls: React.FC<NavigationControlsProps> = ({
178181
navigationViewController.setTrafficIncidentCardsEnabled(isOn);
179182
};
180183

184+
const toggleBackgroundLocationUpdatesEnabled = (isOn: boolean) => {
185+
console.log('toggleBackgroundLocationUpdatesEnabled:', isOn);
186+
setBackgroundLocationUpdatesEnabled(isOn);
187+
navigationViewController.setBackgroundLocationUpdatesEnabled(isOn);
188+
};
189+
181190
const showRouteOverview = () => {
182191
console.log('showRouteOverview');
183192
navigationViewController.showRouteOverview();
@@ -342,6 +351,15 @@ const NavigationControls: React.FC<NavigationControlsProps> = ({
342351
}}
343352
/>
344353
</View>
354+
<View style={styles.rowContainer}>
355+
<Text>Background location updates</Text>
356+
<Switch
357+
value={backgroundLocationUpdatesEnabled}
358+
onValueChange={() => {
359+
toggleBackgroundLocationUpdatesEnabled(!backgroundLocationUpdatesEnabled);
360+
}}
361+
/>
362+
</View>
345363
<View style={styles.rowContainer}>
346364
<Text>Night mode </Text>
347365
<SelectDropdown

components/navigation/navigationView/navigationViewController.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import {NativeModules} from 'react-native';
16+
import {NativeModules, Platform} from 'react-native';
1717
import {LatLng} from '../../shared/types';
1818
import {commands, sendCommand} from '../../shared/viewManager';
1919
import {AudioGuidance, RouteSegment, TimeAndDistance, Waypoint} from '../types';
@@ -112,6 +112,14 @@ export const getNavigationViewController = (
112112
sendCommand(viewId, commands.setAudioGuidanceType, [index]);
113113
},
114114

115+
setBackgroundLocationUpdatesEnabled: (isEnabled: boolean) => {
116+
if (Platform.OS == "ios") {
117+
sendCommand(viewId, commands.setBackgroundLocationUpdatesEnabled,
118+
[isEnabled]
119+
);
120+
}
121+
},
122+
115123
areTermsAccepted: async (): Promise<boolean> => {
116124
return await NavViewModule.areTermsAccepted();
117125
},

components/navigation/navigationView/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,14 @@ export interface NavigationViewController {
452452
*/
453453
setFollowingPerspective(perspective: CameraPerspective): void;
454454

455+
/**
456+
* Enables location updates when the application is on the background.
457+
* Only available in iOS, it's a NO-OP for Android.
458+
*
459+
* @param isEnabled - Determines whether the updates should be enabled or disabled.
460+
*/
461+
setBackgroundLocationUpdatesEnabled(isEnabled: boolean): void;
462+
455463
/**
456464
* Simulator to be used in navigation.
457465
*/

ios/react-native-navigation-sdk/NavViewController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ typedef void (^OnArrayResult)(NSArray *result);
8383
- (void)setZoomGesturesEnabled:(BOOL)isEnabled;
8484
- (void)setBuildingsEnabled:(BOOL)isEnabled;
8585
- (void)setTrafficIncidentCardsEnabled:(BOOL)isEnabled;
86+
- (void)setBackgroundLocationUpdatesEnabled:(BOOL)isEnabled;
8687
- (void)resetMinMaxZoomLevel;
8788
- (void)animateCamera:(GMSCameraUpdate *)update;
8889
- (void)setMapStyle:(GMSMapStyle *)mapStyle;

ios/react-native-navigation-sdk/NavViewController.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,10 @@ - (void)setTrafficIncidentCardsEnabled:(BOOL)isEnabled {
657657
_mapView.settings.showsIncidentCards = isEnabled;
658658
}
659659

660+
- (void)setBackgroundLocationUpdatesEnabled:(BOOL)isEnabled {
661+
_mapView.roadSnappedLocationProvider.allowsBackgroundLocationUpdates = isEnabled;
662+
}
663+
660664
- (void)resetMinMaxZoomLevel {
661665
[_mapView setMinZoom:kGMSMinZoomLevel maxZoom:kGMSMaxZoomLevel];
662666
}

ios/react-native-navigation-sdk/RCTNavViewManager.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ + (BOOL)requiresMainQueueSetup {
146146
});
147147
}
148148

149+
RCT_EXPORT_METHOD(setBackgroundLocationUpdatesEnabled : (nonnull NSNumber *)reactTag isEnabled : (BOOL)isEnabled) {
150+
dispatch_async(dispatch_get_main_queue(), ^{
151+
[viewController setBackgroundLocationUpdatesEnabled:isEnabled];
152+
});
153+
}
154+
149155
RCT_EXPORT_METHOD(startGuidance : (nonnull NSNumber *)reactTag) {
150156
dispatch_async(dispatch_get_main_queue(), ^{
151157
[viewController startGuidance];

0 commit comments

Comments
 (0)