Last active
March 21, 2018 21:06
-
-
Save djalmaaraujo/2e9742d227e67e860e4d485c6f27e511 to your computer and use it in GitHub Desktop.
React Native Geolocation Module
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
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; | |
} |
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
// 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