expo-location
- React Native course
- Advanced
- Lesson 19/83
- 5 min
Learn location access with expo-location.
The expo-location package
To get the user's geographic location you use the expo-location package. This package offers two main functions: current location (taken once) and continuous location (changing over time). With Expo you install it with npx expo install expo-location.
You import the Location namespace from the package: import * as Location from 'expo-location'. Under this namespace you find functions like getCurrentPositionAsync (current location), watchPositionAsync (continuous location) and geocodeAsync (address to coordinate). Location data is sensitive; so asking the user for permission is mandatory.
Import
The expo-location package is imported with import * as Location. This pattern groups all functions of the package under the Location name. Example: import * as Location from 'expo-location'. You then make calls like Location.getCurrentPositionAsync.
The package's functions are asynchronous so they are awaited. The accuracy of location info depends on the device's hardware and permission settings. When GPS is on, accuracy is high; when off, it is low. This package offers a consistent interface on both Android and iOS; you do not need to write platform-specific code.
Practice Import the package
Complete the empty file. Import the expo-location package with import * as Location. Export it with export { Location }.
Starter code
// import the expo-location package
// export itSample solution
import * as Location from 'expo-location';
export { Location };Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does expo-location provide?
- Only camera
- Only network requests
- Only file storage
- Location info Correct answer
Explanation: The package provides location info.
2 Write the import statement
import * as ____ from 'expo-location';
import * as Location from 'expo-location';
Explanation: The Location namespace carries all functions.
3 Match each part to its role
| Term | Meaning |
|---|---|
getCurrentPositionAsync |
Current location |
watchPositionAsync |
Continuous location |
requestForegroundPermissionsAsync |
Asks permission |
geocodeAsync |
Address to coordinate |