-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
88 lines (82 loc) · 2.25 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import React, { Component } from 'react';
import LocationAndRZ from './screens/LocationAndRZ';
import * as Location from 'expo-location';
import * as Permissions from 'expo-permissions';
import * as TaskManager from 'expo-task-manager';
import regions from './regions/regions'
import { View, Text } from 'react-native';
export default class App extends Component{
state = {
mapRegion: {
latitude: 0,
longitude: 0,
latitudeDelta: 0.002,
longitudeDelta: 0.002,
},
hasLocationPermissions: false,
locationResult: null,
location: null,
};
componentDidMount = () => {
this.getLocationAsync();
Location.startGeofencingAsync("isUserInRZ", regions)
}
startTasks = () => {
TaskManager.defineTask("isUserInRZ", ({ data: { eventType, region }, error }) => {
if (error) {
console.log("ERROR");
return;
}
if (eventType === Location.GeofencingEventType.Enter) {
console.log("You've entered region:", region);
} else if (eventType === Location.GeofencingEventType.Exit) {
console.log("You've left region:", region);
}
});
TaskManager.defineTask("getLocationUpdates", ({ data: { locations }, error }) => {
if (error) {
console.log("ERROR");
return;
}
let location = Object.values({...locations})[0].coords
this.setState({
location
})
this.setState({
mapRegion: {
latitude: location.latitude,
longitude: location.longitude,
latitudeDelta: 0.05,
longitudeDelta: 0.05,
},
})
})
}
handleMapRegionChange = mapRegion => {
this.setState({ mapRegion });
};
async getLocationAsync () {
const { status, permissions } = await Permissions.askAsync(
Permissions.LOCATION
);
if (status === 'granted') {
this.setState({ hasLocationPermissions: true });
Location.startLocationUpdatesAsync('getLocationUpdates')
} else {
alert('Location permission not granted');
}
};
render(){
this.startTasks()
return this.state.location ? (
<LocationAndRZ
mapRegion={this.state.mapRegion}
latitude={this.state.location.latitude}
longitude={this.state.location.longitude}
location={this.state.location}
/>
) : (
<Text>LOADING</Text>
)
}
}