72 lines
2.4 KiB
JavaScript
72 lines
2.4 KiB
JavaScript
import React, { Component } from 'react'
|
|
import { View, Text, Switch, StyleSheet } from 'react-native'
|
|
import { PermissionsAndroid } from 'react-native';
|
|
|
|
|
|
class SwichExample extends Component {
|
|
constructor(props) {
|
|
super(props);
|
|
|
|
this.state = {
|
|
latitude: null,
|
|
longitude: null,
|
|
error: null,
|
|
};
|
|
}
|
|
async requestLocationPermission() {
|
|
const chckLocationPermission = PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
|
|
if (chckLocationPermission === PermissionsAndroid.RESULTS.GRANTED) {
|
|
alert("You've access for the location");
|
|
} else {
|
|
try {
|
|
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
|
|
{
|
|
'title': 'Cool Location App required Location permission',
|
|
'message': 'We required Location permission in order to get device location ' +
|
|
'Please grant us.'
|
|
}
|
|
)
|
|
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
|
|
alert("You've access for the location");
|
|
} else {
|
|
alert("You don't have access for the location");
|
|
}
|
|
} catch (err) {
|
|
alert(err)
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
navigator.geolocation.getCurrentPosition(
|
|
(position) => {
|
|
this.setState({
|
|
latitude: position.coords.latitude,
|
|
longitude: position.coords.longitude,
|
|
error: null,
|
|
});
|
|
},
|
|
(error) => this.setState({ error: error.message }),
|
|
{ enableHighAccuracy: false, timeout: 20000, maximumAge: 1000 },
|
|
);
|
|
}
|
|
|
|
render() {
|
|
|
|
return (
|
|
<View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
|
|
<Text
|
|
onPress={() => this.requestLocationPermission()}>
|
|
Request Location Permission
|
|
</Text>
|
|
<Text>Latitude: {this.state.latitude}</Text>
|
|
<Text style={{paddingBottom: 10}}>Longitude: {this.state.longitude}</Text>
|
|
{this.state.error ? <Text>Error: {this.state.error}</Text> : null}
|
|
</View>
|
|
)
|
|
}
|
|
}
|
|
export default SwichExample;
|