Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Encapsulating location
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