I want to know about the status of the GPS during the use of the app.This means that if the user deactivates the phone GPS, I warn him to turn on the GPS. At the moment, I can only check the status of the GPS at the start of run the app.
import React, { Component } from 'react';
import { Platform, Text, View, StyleSheet } from 'react-native';
import { Constants, Location, Permissions } from 'expo';
import { connect } from 'react-redux';
import { setGps } from '../actions'
class GpsSensor extends Component {
state = {
location: null,
errorMessage: null,
gps:false,
};
componentWillMount() {
if (Platform.OS === 'android' && !Constants.isDevice) {
this.setState({
errorMessage: 'Oops, this will not work on Sketch in an Android emulator. Try it on your device!',
});
} else {
this.getLocation();
}
}
componentDidMount() {
this._getGpsStatus();
}
_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
errorMessage: 'Permission to access location was denied',
});
}
let location = await Location.getCurrentPositionAsync({});
this.setState({ location });
};
getLocation = () => {
this.mlocation = Expo.Location.watchPositionAsync({
enableHighAccuracy: true,
timeInterval: 1,
distanceInterval: 1
}, (loc) => {
if (loc.timestamp) {
this.props.dispatch(setGps(loc));
this.setState({location:loc});
} else {
this.setState({errorMessage:'Problems on update location'})
}
});
}
_getGpsStatus= async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
errorMessage: 'Permission to access location was denied',
});
}
let gps = await Location.getProviderStatusAsync();
this.setState({ gps: gps.gpsAvailable });
console.log(gps.gpsAvailable)
};
render() {
let text = 'Waiting..';
if (this.state.errorMessage) {
text = this.state.errorMessage;
} else if (this.state.location) {
text = JSON.stringify(this.state.location);
}
let mGps;
if (this.state.gps) {
mGps="true"
}
else mGps="false"
return (
<View style={styles.container}>
<Text> Gps Sensor : </Text>
<Text style={styles.paragraph}>{text}</Text>
<Text> "this.state.gps": {mGps} </Text>
</View>
);
}
}
GpsSensor = connect()(GpsSensor);
export default GpsSensor
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
},
paragraph: {
margin: 24,
fontSize: 18,
textAlign: 'center',
},
});