use-device-location
v0.1.0
Published
React Native/Expo hook for device location with status tracking, a derived 'show enable-location prompt' flag, optional periodic re-checks, and pluggable reverse geocoding (Expo, Nominatim, Google, or your own).
Maintainers
Readme
use-device-location
React Native/Expo hook for device location: permission + services handling, a derived "show enable-location prompt" flag, optional periodic re-checks, and pluggable reverse geocoding.
Install
npm install use-device-location expo-locationBasic usage (no geocoding)
import { useDeviceLocation } from "use-device-location";
const { location, status, showLocationPrompt, checkLocationIfOff } =
useDeviceLocation({ geocoder: "none" });With reverse geocoding (device-native, default)
const { location } = useDeviceLocation();
// location.city, location.formattedAddress, etc.With Nominatim (free, no key — requires a User-Agent per their usage policy)
const { location } = useDeviceLocation({
geocoder: "nominatim",
nominatim: { userAgent: "MyApp/1.0 ([email protected])" },
});With Google Geocoding API, falling back to the device geocoder
const { location } = useDeviceLocation({
geocoder: "google",
google: { apiKey: process.env.MY_GOOGLE_MAPS_KEY! },
fallbackGeocoder: "expo",
});Periodic re-checks (off by default)
const { showLocationPrompt, checkLocationIfOff } = useDeviceLocation({
refreshIntervalMs: 5 * 60 * 1000, // re-check every 5 min, forever
});Manual click handler (no-ops unless location is currently off)
<Button title="Enable location" onPress={checkLocationIfOff} />
{showLocationPrompt && <LocationOffBanner />}Bring your own geocoder
const { location } = useDeviceLocation({
geocoder: async (lat, lng) => {
// call any service you want
return { city: "...", formattedAddress: "..." };
},
});API
Options
| Option | Type | Default |
| -------------------- | ----------------------------- | -------- |
| autoStart | boolean | true |
| geocoder | "none" \| "expo" \| "nominatim" \| "google" \| (lat,lng)=>Promise<...> | "expo" |
| nominatim | { userAgent, baseUrl? } | — |
| google | { apiKey } | — |
| fallbackGeocoder | same shape as geocoder | — |
| accuracy | Location.LocationAccuracy | Balanced |
| refreshIntervalMs | number | never auto-rechecks |
Return value
status, location, error, loading, refresh(), openSettings(),
showLocationPrompt, checkLocationIfOff().
