Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import {
  3. Platform,
  4. Text,
  5. View,
  6. StyleSheet,
  7. Linking,
  8. Button,
  9. AppState
  10. } from "react-native";
  11. import Constants from "expo-constants";
  12. import * as TaskManager from "expo-task-manager";
  13. import * as Location from "expo-location";
  14. import * as Permissions from "expo-permissions";
  15. import * as IntentLauncher from "expo-intent-launcher";
  16. import { Alert, NavigationActions } from "react-native";
  17. import Modal from "react-native-modal";
  18.  
  19. const LOCATION_TASK_NAME = "background-location-task";
  20.  
  21. export default class LocationScreen extends Component {
  22. state = {
  23. location: null,
  24. errorMessage: null,
  25. };
  26.  
  27.  
  28. componentDidMount = async () => {
  29. await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
  30. accuracy: Location.Accuracy.Balanced
  31. });
  32.  
  33.  
  34.  
  35.  
  36. render() {
  37. let text = "Waiting..";
  38. if (this.state.errorMessage) {
  39. text = this.state.errorMessage;
  40. } else if (this.state.location) {
  41. text = JSON.stringify(this.state.location);
  42. }
  43.  
  44. return (
  45. <View style={styles.container}>
  46. <Text style={styles.paragraph}>{text}</Text>
  47. </View>
  48. );
  49. }
  50. }
  51.  
  52. TaskManager.defineTask(LOCATION_TASK_NAME, handel);
  53.  
  54. export async function handel({ data, error }) {
  55. try {
  56. let { status } = await Permissions.askAsync(Permissions.LOCATION);
  57.  
  58. if (status !== "granted") {
  59. this.setState({
  60. errorMessage: "Permission to access location was denied"
  61. });
  62. return;
  63. } else {
  64. if (data) {
  65. const { locations } = data;
  66. const location = locations.map(location => {
  67. let newLocation = Location.reverseGeocodeAsync(location.coords).then(
  68. data => {
  69. let maplocation = data.map(location => {
  70. console.log(location);
  71. console.log(location.city);
  72. });
  73. }
  74. );
  75. });
  76. }
  77. }
  78. } catch (error) {
  79. let status = Location.getProviderStatusAsync();
  80. if (!status.locationServiceEnabled) {
  81. const { navigation } = this.props;
  82. navigation.navigate("Login");
  83. }
  84. }
  85. }
  86.  
  87. const styles = StyleSheet.create({
  88. container: {
  89. flex: 1,
  90. alignItems: "center",
  91. justifyContent: "center",
  92. paddingTop: Constants.statusBarHeight,
  93. backgroundColor: "#ecf0f1"
  94. },
  95. paragraph: {
  96. margin: 24,
  97. fontSize: 18,
  98. textAlign: "center"
  99. }
  100. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement