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 ( this.requestLocationPermission()}> Request Location Permission Latitude: {this.state.latitude} Longitude: {this.state.longitude} {this.state.error ? Error: {this.state.error} : null} ) } } export default SwichExample;