I am creating an app using react native. I would like to get the users location using react-native-geolocation-service .
I have followed the setup as documented on GitHub. When I try to run my app within iOS I get the following error.
ReferenceError: Can't find variable: hasLocationPermission
Here is my code
const hasLocationPermission = async () => { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.LOCATION, { title: 'Cool Weather App', message: 'Cool Weather App needs access to use your location', buttonNegative: 'Ask Me Later', buttonNegative: 'Cancel', buttonPositive: 'OK' } ); if (granted === PermissionsAndroid.RESULTS.GRANTED) { console.log('You can use the app'); } else { console.log('Location Permission Denied'); } (error) => { console.warn(error); } } componentDidMount() { if(hasLocationPermission) { Geolocation.getCurrentPosition( (position) => { console.log(position); }, (error) => { console.log(error.code, error.message); }, {enableHighAccuracy: true, timeout: 15000, maximumAge: 10000} ); } }
I'm not sure if I'm missing code or doing something wrong.
UPDATE:
I have fixed the Reference Error and am now facing a new error.
ERROR Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
You can check location permission like this:
class deneme extends Component {
hasLocationPermission = async() => {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.LOCATION, {
title: 'Cool Weather App',
message: 'Cool Weather App needs access to use your location',
buttonNegative: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK'
});
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('You can use the app');
return true;
} else {
console.log('Location Permission Denied');
return false;
}
}
constructor(props) {
super(props);
this.state = {
_isLocationEnabled: false
};
}
componentDidMount() {
hasLocationPermission().then(result => {
this.setState({_isLocationEnabled: result});
});
}
render() {
{ this.state._isLocationEnabled ? (
<View><Text>Permission Enabled!</Text></View>
) : ( <View><Text>Permission Disabled!</Text></View> )}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.