Created
July 6, 2020 02:47
-
-
Save faustoct1/0f321954d20d202cc8e39e9e1b5e6074 to your computer and use it in GitHub Desktop.
Encapsulating 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 { Platform, PermissionsAndroid } from 'react-native' | |
import { Constants, Permissions } from 'react-native-unimodules' | |
import NetInfo from "@react-native-community/netinfo"; | |
import * as L from 'expo-location' | |
class Location { | |
static getAsync = async () => { | |
if (Platform.OS === 'android' && !Constants.isDevice) { | |
throw new Exception('Geolocation only in devices not emulators.') | |
} | |
try{ | |
let location = {} | |
const {status: granted} = await Permissions.getAsync(Permissions.LOCATION); | |
const info = await NetInfo.getConnectionInfo() | |
if(granted === 'granted'){ | |
location = await L.getCurrentPositionAsync({}) | |
return {status: granted, location: location, granted: (granted === 'granted')} | |
} | |
const {status} = await Permissions.askAsync(Permissions.LOCATION) | |
if(status === 'granted'){ | |
location = await L.getCurrentPositionAsync({}) | |
return {status: status, location: location, granted: (status === 'granted')} | |
} | |
return {status: status, location: location, granted: (status === 'granted')} | |
}catch(e){ | |
//alert(e) | |
} | |
return null | |
} | |
} | |
export default Location; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment