Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { Platform, Text, View, StyleSheet } from 'react-native';
  3. import Constants from 'expo-constants';
  4. import * as Location from 'expo-location';
  5. import * as Permissions from 'expo-permissions';
  6.  
  7. export default class App extends Component {
  8. state = {
  9. location: null,
  10. errorMessage: null,
  11. };
  12.  
  13. componentWillMount() {
  14. if (Platform.OS === 'android' && !Constants.isDevice) {
  15. this.setState({
  16. errorMessage: 'Oops, this will not work on Sketch in an Android emulator. Try it on your device!',
  17. });
  18. } else {
  19. this._getLocationAsync();
  20. }
  21. }
  22.  
  23. _getLocationAsync = async () => {
  24. let { status } = await Permissions.askAsync(Permissions.LOCATION);
  25. if (status !== 'granted') {
  26. this.setState({
  27. errorMessage: 'Permission to access location was denied',
  28. });
  29. }
  30.  
  31. let location = await Location.getCurrentPositionAsync({});
  32. this.setState({ location });
  33. };
  34.  
  35. render() {
  36. let text = 'Waiting..';
  37. if (this.state.errorMessage) {
  38. text = this.state.errorMessage;
  39. } else if (this.state.location) {
  40. text = JSON.stringify(this.state.location);
  41. }
  42.  
  43. return (
  44. <View style={styles.container}>
  45. <Text style={styles.paragraph}>{text}</Text>
  46. </View>
  47. );
  48. }
  49. }
  50.  
  51. const styles = StyleSheet.create({
  52. container: {
  53. flex: 1,
  54. alignItems: 'center',
  55. justifyContent: 'center',
  56. paddingTop: Constants.statusBarHeight,
  57. backgroundColor: '#ecf0f1',
  58. },
  59. paragraph: {
  60. margin: 24,
  61. fontSize: 18,
  62. textAlign: 'center',
  63. },
  64. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement