Last active
October 10, 2020 11:52
-
-
Save ourmaninamsterdam/fe202690455917b7c660e9ca059e59ca to your computer and use it in GitHub Desktop.
Reports LocationProviderStatus (GPS/network or passive location Android only) from expo-location
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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