Last active
May 1, 2020 23:24
-
-
Save renielsalvador/c22354188dd91803b5e751d7edfd8188 to your computer and use it in GitHub Desktop.
GeoLocation enabler ios android
This file contains 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 RNAndroidLocationEnabler from 'react-native-android-location-enabler'; | |
import {Platform, PermissionsAndroid} from 'react-native'; | |
import Geolocation from 'react-native-geolocation-service'; | |
export const checkLocationPermission = async (): Promise<string> => { | |
if (Platform.OS === 'android') { | |
const permission = await PermissionsAndroid.request( | |
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, | |
); | |
return new Promise((resolve, reject) => { | |
if (permission === PermissionsAndroid.RESULTS.GRANTED) { | |
return resolve('permission granted'); | |
} else { | |
reject(new Error('Refused the permission ACCESS_FINE_LOCATION')); | |
} | |
}); | |
} | |
return Promise.resolve('permission granted'); | |
}; | |
export const enableLocation = (): void => { | |
if (Platform.OS === 'ios') { | |
Geolocation.requestAuthorization(); | |
} else { | |
RNAndroidLocationEnabler.promptForEnableLocationIfNeeded({ | |
interval: 10000, | |
fastInterval: 5000, | |
}) | |
.then(() => { | |
console.log('connected'); | |
}) | |
.catch(() => { | |
console.log('failed'); | |
}); | |
} | |
}; | |
export default { | |
checkLocationPermission, | |
enableLocation, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment