Skip to content

Instantly share code, notes, and snippets.

@ourmaninamsterdam
Last active October 10, 2020 11:52
Show Gist options
  • Save ourmaninamsterdam/fe202690455917b7c660e9ca059e59ca to your computer and use it in GitHub Desktop.
Save ourmaninamsterdam/fe202690455917b7c660e9ca059e59ca to your computer and use it in GitHub Desktop.
Reports LocationProviderStatus (GPS/network or passive location Android only) from expo-location
import { getProviderStatusAsync, LocationProviderStatus } from 'expo-location';
import { useState } from 'react';
import { Platform } from 'react-native';
import useInterval from 'use-interval';
const useLocationProviderStatus = (delay: number = 2000) => {
const [locationProviderAvailable, setLocationProviderAvailable] = useState<boolean | undefined>(undefined);
const getLocationProviderStatus = ({
gpsAvailable,
networkAvailable,
passiveAvailable,
}: LocationProviderStatus): boolean | undefined => {
if (Platform.OS === 'ios') return undefined;
return gpsAvailable || networkAvailable || passiveAvailable;
};
useInterval(async () => {
const providerStatus = await getProviderStatusAsync();
setLocationProviderAvailable(getLocationProviderStatus(providerStatus));
}, delay);
return { locationProviderAvailable };
};
export default useLocationProviderStatus;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment