Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
React Native Geolocation Module
compile(project(':react-native-geolocation-service')){
exclude group: 'com.google.android.gms' // this is important
}
compile ("com.google.android.gms:play-services-base:11.0.0") {
force = true;
}
compile ("com.google.android.gms:play-services-location:11.0.0") {
force = true;
}
compile ("com.google.android.gms:play-services-maps:11.0.0") {
force = true;
}
compile ("com.google.android.gms:play-services-gcm:11.0.0") {
force = true;
}
// Packages: "react-native-geolocation-service": "^1.0.3",
// Config.log is a shortcut to console.log
import Geolocation from 'react-native-geolocation-service'
import { PermissionsAndroid, Platform } from 'react-native'
import Config from '../Config/Config'
const GRANTED_PERMISSION_VALUE = 'granted'
export const getLocation = async () => {
return new Promise((res, rej) => {
requestLocationPermission().then((granted) => {
if (granted !== getPermissionDroid()) {
return res({
lat: 0,
long: 0
})
}
return Geolocation.getCurrentPosition(
(position) => {
Config.log('GEOLOCATION', position)
res({
lat: position.coords.latitude,
long: position.coords.longitude
})
},
(error) => {
Config.log('GEOLOCATION - Error:', error)
rej(error)
},
{
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 10 // ms
}
)
})
})
}
const requestLocationPermission = () => {
if (Platform.OS === 'ios') {
return Promise.resolve(GRANTED_PERMISSION_VALUE)
}
const permission = PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
return PermissionsAndroid.check(permission).then((isAlreadyGranted) => {
Config.log('isAlreadyGranted', isAlreadyGranted)
return PermissionsAndroid.request(
permission,
{
'title': 'Sua localização é necessária para o PlaceCar',
'message': 'PlaceCar precisa do acesso a sua localização para mostrar os postos mais próximos'
}
)
})
}
const getPermissionDroid = () => {
if (Platform.OS === 'ios') {
Config.log('PERMISSION/IOS detected. No check needed, returning default value')
return GRANTED_PERMISSION_VALUE
}
Config.log('PermissionsAndroid.RESULTS.GRANTED', PermissionsAndroid.RESULTS.GRANTED)
return PermissionsAndroid.RESULTS.GRANTED
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.