useLocation
Source:hooks/useLocation.ts (102 lines)
The useLocation hook manages GPS position tracking with a three-branch permission handling strategy that adapts to the user’s current permission state.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
GPS position tracking with three-branch permission handling.
hooks/useLocation.ts (102 lines)
The useLocation hook manages GPS position tracking with a three-branch permission handling strategy that adapts to the user’s current permission state.
getForegroundPermissionsAsync()
│
├── CASE A: status === "granted"
│ → Fetch location immediately
│
├── CASE B: status !== "granted" AND canAskAgain === true
│ → Show system permission dialog
│ → If granted, fetch location
│
└── CASE C: status !== "granted" AND canAskAgain === false
→ Permission permanently blocked
→ Show Alert with "Open Settings" button
→ Deep-link to Android Settings via Linking.openSettings()
const location = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.High,
});
const coords = await requestLocation((coords) => {
mapRef.current?.animateCamera({ center: coords });
});
| Field | Type | Purpose |
|---|---|---|
| hasPermission | boolean or null | null = not yet checked, true = granted, false = denied |
| currentLocation | LocationCoords or null | Latest GPS coordinates |
| isLocating | boolean | True while a position request is in flight |
| requestLocation | function | Triggers the permission check and location fetch |
interface LocationCoords {
latitude: number;
longitude: number;
}