Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the React Native Geolocation API.
React Native
import Geolocation from '[1]';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing from 'expo-location' without using Expo.
Using '@react-native-community/geolocation' which is deprecated.
✗ Incorrect
The correct package to import for React Native Geolocation is react-native-geolocation-service.
2fill in blank
mediumComplete the code to request location permission on Android.
React Native
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.[1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Requesting ACCESS_NETWORK_STATE which is unrelated to location.
Using ACCESS_COARSE_LOCATION when fine location is needed.
✗ Incorrect
To get precise location, you request ACCESS_FINE_LOCATION permission on Android.
3fill in blank
hardFix the error in the code to get current position.
React Native
Geolocation.getCurrentPosition([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing null or error callback as first argument.
Using a callback that does not accept the position parameter.
✗ Incorrect
The first argument to getCurrentPosition is a success callback that receives the position object.
4fill in blank
hardFill both blanks to watch location changes and handle errors.
React Native
const watchId = Geolocation.watchPosition([1], [2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping success and error callbacks.
Passing null instead of error callback.
✗ Incorrect
The first argument is the success callback for position updates, the second is the error callback.
5fill in blank
hardFill all three blanks to clear the location watch and stop updates.
React Native
Geolocation.[1](watchId); // To stop watching, call [2] with the [3] returned by watchPosition.
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using stopWatching which does not exist.
Not passing the watchId to clearWatch.
✗ Incorrect
You use clearWatch with the watchId to stop location updates.