react native get user current location code example
Example 1: React native user based location
import { NativeModules, DeviceEventEmitter, PermissionsAndroid } from 'react-native' import Config from 'react-native-config' import get from 'lodash/get' const { GeoLocation } = NativeModulesclass BackgroundGeoLocation { constructor(token, user_id) { this.state = null } start(dispatch, nextState) { this.dispatch = dispatch const token = get(nextState, 'session.data.token') const user_id = get(nextState, 'user.data.user_id') const id = get(nextState, 'user.data.id') this.state = { user_id, token, } return PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION) .then(is_granted => is_granted === PermissionsAndroid.RESULTS.GRANTED ? is_granted : PermissionsAndroid.requestMultiple([ PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION, ]) ) .then(_ => PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION)) .then(is_granted => is_granted === PermissionsAndroid.RESULTS.GRANTED ? true : new Error()) .then(_ => setTimeout(() => GeoLocation.startService(token, user_id, id, `${Config.API_URL}/live/car-tracking/gps-pos/`), 300)) .catch(e => console.log(e)) } stop() { return GeoLocation.stopService() .then(_ => console.log(_)) } handleLocationChange(geo) { console.log(geo) }}export default BackgroundGeoLocation
Example 2: get user location in react native
npm i -S react-native-get-location