Skip to content

Commit 9bb8591

Browse files
authored
chore: include navigation initialization in readme (#330)
1 parent 1c45b1c commit 9bb8591

File tree

3 files changed

+151
-25
lines changed

3 files changed

+151
-25
lines changed

README.md

Lines changed: 147 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,138 @@ To set up, specify your API key in the application delegate `ios/Runner/AppDeleg
6565
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
6666
{
6767
[GMSServices provideAPIKey:@"API_KEY"];
68-
[GMSServices setMetalRendererEnabled:YES];
6968
return [super application:application didFinishLaunchingWithOptions:launchOptions];
7069
}
7170

7271
```
7372
7473
## Usage
7574
75+
### Initializing Navigation
76+
Wrap application with the `NavigationProvider` component. This will provide the necessary context for navigation throughout your app.
77+
78+
```tsx
79+
import React from 'react';
80+
import {
81+
NavigationProvider,
82+
TaskRemovedBehavior,
83+
type TermsAndConditionsDialogOptions,
84+
} from '@googlemaps/react-native-navigation-sdk';
85+
86+
const termsAndConditionsDialogOptions: TermsAndConditionsDialogOptions = {
87+
title: 'Terms and Conditions Title',
88+
companyName: 'Your Company Name',
89+
showOnlyDisclaimer: true,
90+
};
91+
92+
const App = () => {
93+
return (
94+
<NavigationProvider
95+
termsAndConditionsDialogOptions={termsAndConditionsDialogOptions}
96+
taskRemovedBehavior={TaskRemovedBehavior.CONTINUE_SERVICE}
97+
>
98+
{/* Add your application components here */}
99+
</NavigationProvider>
100+
);
101+
};
102+
103+
export default App;
104+
```
105+
106+
#### Task Removed Behavior
107+
108+
The `taskRemovedBehavior` prop defines how the navigation should behave when a task is removed from the recent apps list on Android. It can either:
109+
110+
- `CONTINUE_SERVICE`: Continue running in the background. (default)
111+
- `QUIT_SERVICE`: Shut down immediately.
112+
113+
This prop has only an effect on Android.
114+
115+
### Using NavigationController
116+
117+
You can use the `useNavigation` hook to access the `NavigationController` and control navigation within your components. The `useNavigation` hook also provides methods to add and remove listeners.
118+
119+
#### Initializing Navigation
120+
121+
```tsx
122+
...
123+
const { navigationController } = useNavigation();
124+
125+
const initializeNavigation = useCallback(async () => {
126+
try {
127+
await navigationController.init();
128+
console.log('Navigation initialized');
129+
} catch (error) {
130+
console.error('Error initializing navigation', error);
131+
}
132+
}, [navigationController]);
133+
```
134+
135+
> [!NOTE]
136+
> Navigation can be controlled separately from the navigation views allowing navigation to be started and stopped independently.
137+
138+
139+
#### Starting Navigation
140+
To start navigation, set a destination and start guidance:
141+
142+
```tsx
143+
try {
144+
const waypoint = {
145+
title: 'Destination',
146+
position: {
147+
lat: 37.4220679,
148+
lng: -122.0859545,
149+
},
150+
};
151+
152+
const routingOptions = {
153+
travelMode: TravelMode.DRIVING,
154+
avoidFerries: false,
155+
avoidTolls: false,
156+
};
157+
158+
await navigationController.setDestinations([waypoint], routingOptions);
159+
await navigationController.startGuidance();
160+
} catch (error) {
161+
console.error('Error starting navigation', error);
162+
}
163+
164+
```
165+
166+
167+
#### Adding navigation listeners
168+
169+
```tsx
170+
const { navigationController, addListeners, removeListeners } = useNavigation();
171+
172+
const onArrival = useCallback((event: ArrivalEvent) => {
173+
if (event.isFinalDestination) {
174+
console.log('Final destination reached');
175+
navigationController.stopGuidance();
176+
} else {
177+
console.log('Continuing to the next destination');
178+
navigationController.continueToNextDestination();
179+
navigationController.startGuidance();
180+
}
181+
}, [navigationController]);
182+
183+
const navigationCallbacks = useMemo(() => ({
184+
onArrival,
185+
// Add other callbacks here
186+
}), [onArrival]);
187+
188+
useEffect(() => {
189+
addListeners(navigationCallbacks);
190+
return () => {
191+
removeListeners(navigationCallbacks);
192+
};
193+
}, [navigationCallbacks, addListeners, removeListeners]);
194+
```
195+
196+
See `NavigationCallbacks` interface for a list of available callbacks.
197+
198+
When removing listeners, ensure you pass the same object that was used when adding them, as multiple listeners can be registered for the same event.
199+
76200
### Add a navigation view
77201

78202
You can now add a `NavigationView` component to your application..
@@ -83,24 +207,24 @@ The `NavigationView` compoonent should be used within a View with a bounded size
83207
in an unbounded widget will cause the application to behave unexpectedly.
84208

85209
```tsx
86-
// Permissions must have been granted by this point.
87-
88-
<NavigationView
89-
androidStylingOptions={{
90-
primaryDayModeThemeColor: '#34eba8',
91-
headerDistanceValueTextColor: '#76b5c5',
92-
headerInstructionsFirstRowTextSize: '20f',
93-
}}
94-
iOSStylingOptions={{
95-
navigationHeaderPrimaryBackgroundColor: '#34eba8',
96-
navigationHeaderDistanceValueTextColor: '#76b5c5',
97-
}}
98-
navigationViewCallbacks={navigationViewCallbacks}
99-
mapViewCallbacks={mapViewCallbacks}
100-
onMapViewControllerCreated={setMapViewController}
101-
onNavigationViewControllerCreated={setNavigationViewController}
102-
termsAndConditionsDialogOptions={termsAndConditionsDialogOptions}
103-
/>
210+
// Permissions must have been granted by this point.
211+
212+
<NavigationView
213+
androidStylingOptions={{
214+
primaryDayModeThemeColor: '#34eba8',
215+
headerDistanceValueTextColor: '#76b5c5',
216+
headerInstructionsFirstRowTextSize: '20f',
217+
}}
218+
iOSStylingOptions={{
219+
navigationHeaderPrimaryBackgroundColor: '#34eba8',
220+
navigationHeaderDistanceValueTextColor: '#76b5c5',
221+
}}
222+
navigationViewCallbacks={navigationViewCallbacks}
223+
mapViewCallbacks={mapViewCallbacks}
224+
onMapViewControllerCreated={setMapViewController}
225+
onNavigationViewControllerCreated={setNavigationViewController}
226+
termsAndConditionsDialogOptions={termsAndConditionsDialogOptions}
227+
/>
104228
```
105229

106230
### Add a map view
@@ -114,8 +238,6 @@ You can also add a bare `MapView` that works as a normal map view without naviga
114238
/>
115239
```
116240

117-
See the [example](./example) directory for a complete navigation sample app.
118-
119241
### Requesting and handling permissions
120242

121243
The Google Navigation SDK React Native library offers functionalities that necessitate specific permissions from the mobile operating system. These include, but are not limited to, location services, background execution, and receiving background location updates.
@@ -159,6 +281,10 @@ By default, `NavigationView` uses all the available space provided to it. To adj
159281
/>
160282
```
161283

284+
### Sample application
285+
286+
See the [example](./example) directory for a complete navigation sample app.
287+
162288
## Support for Android Auto and Apple CarPlay
163289
This plugin is compatible with both Android Auto and Apple CarPlay infotainment systems. For more details, please refer to the respective platform documentation:
164290

example/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import * as React from 'react';
17+
import React from 'react';
1818
import {
1919
NavigationContainer,
2020
useIsFocused,
@@ -68,7 +68,7 @@ const Stack = createStackNavigator();
6868

6969
/**
7070
* Root component of the application.
71-
* @return {NavigationContainer} The NavigationContainer as a root component.
71+
* @return {NavigationProvider} The NavigationProvider as a root component.
7272
*/
7373
export default function App() {
7474
return (

example/src/controls/navigationControls.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ const NavigationControls: React.FC<NavigationControlsProps> = ({
127127
placeId: 'ChIJkXCsHWSAhYARsGBBQYcj-V0', // 1 Market st, SF
128128
};
129129

130-
const map = [wp1, wp2];
130+
const waypoints = [wp1, wp2];
131131

132132
const routingOptions: RoutingOptions = {
133133
travelMode: TravelMode.DRIVING,
134134
avoidFerries: true,
135135
avoidTolls: false,
136136
};
137137

138-
navigationController.setDestinations(map, routingOptions);
138+
navigationController.setDestinations(waypoints, routingOptions);
139139
};
140140

141141
const setFollowingPerspective = (index: CameraPerspective) => {

0 commit comments

Comments
 (0)