Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { Component } from 'react';
- import { Platform, Text, View, StyleSheet } from 'react-native';
- import Constants from 'expo-constants';
- import * as Location from 'expo-location';
- import * as Permissions from 'expo-permissions';
- 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',
- },
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement