diff options
| author | Andrew <andrew.mihai@mail.utoronto.ca> | 2019-03-05 14:33:54 +0000 |
|---|---|---|
| committer | Andrew <andrew.mihai@mail.utoronto.ca> | 2019-03-05 14:33:54 +0000 |
| commit | e40e046cc7f62db42d0b634870da4de020e9108a (patch) | |
| tree | 1f3f7068bafd68a4e34ff776fee325ecfee21606 /mobile/components | |
| parent | 8a5bf1ba34a2ef52fd8ebc10e3b7b0a2e7360e55 (diff) | |
Added location functionality to greeting page. Location data is now sent
to the form page after the 'get location' button is pressed.
Diffstat (limited to 'mobile/components')
| -rw-r--r-- | mobile/components/GreetingPage.js | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/mobile/components/GreetingPage.js b/mobile/components/GreetingPage.js index 8b87474..5e6d502 100644 --- a/mobile/components/GreetingPage.js +++ b/mobile/components/GreetingPage.js @@ -1,10 +1,55 @@ import React, { Component } from 'react'; -import { Text, View, Alert, StyleSheet, Button, AppRegistry, Navigator, TouchableHighlight} from 'react-native'; +import { Text, View, Alert, StyleSheet, Button, AppRegistry, Navigator, TouchableHighlight, Platform} from 'react-native'; import { Ionicons } from '@expo/vector-icons'; import { createStackNavigator, createAppContainer } from 'react-navigation'; +import { Location, Permissions, Constants } from 'expo'; export default class GreetingPage extends Component { //Every component needs a render() function + 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 }); + }; + + getCurrentLocation = () => { + + navigator.geolocation.getCurrentPosition( + position => { + this.recorded_coordinates = JSON.stringify([position.coords.latitude, position.coords.longitude]); + }, + ); + } + + moveToFormPage = () => { + this._getLocationAsync; + const coordinates = [this.state.location.coords.longitude, this.state.location.coords.latitude] + // Alert.alert(JSON.stringify(coordinates)); + this.props.navigation.navigate('FormPage', {coordinates : coordinates}); + } + + render() { return ( //<View> acts like the way <div> does in JavaScript @@ -22,7 +67,8 @@ export default class GreetingPage extends Component { <TouchableHighlight style={styles.first_button_container} - onPress={() => this.props.navigation.navigate('FormPage')}> + onPress={() => { this.moveToFormPage() + }}> <View style={styles.press_button}> |
