Created
March 30, 2018 02:23
-
-
Save phpsmarter/6a2f8baa609fbc42bf40057f7edf0052 to your computer and use it in GitHub Desktop.
expo method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { Platform, Text, View, StyleSheet } from 'react-native'; | |
import { Constants, Location, Permissions } from 'expo'; | |
export default class App extends Component { | |
state = { | |
location: null, | |
errorMessage: null, | |
}; | |
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._getLocationAsync(); | |
} | |
} | |
_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 }); | |
}; | |
render() { | |
let text = 'Waiting..'; | |
if (this.state.errorMessage) { | |
text = this.state.errorMessage; | |
} else if (this.state.location) { | |
text = JSON.stringify(this.state.location); | |
} | |
return ( | |
<View style={styles.container}> | |
<Text style={styles.paragraph}>{text}</Text> | |
</View> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
alignItems: 'center', | |
justifyContent: 'center', | |
paddingTop: Constants.statusBarHeight, | |
backgroundColor: '#ecf0f1', | |
}, | |
paragraph: { | |
margin: 24, | |
fontSize: 18, | |
textAlign: 'center', | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment