-
Notifications
You must be signed in to change notification settings - Fork 12
/
App.js
148 lines (126 loc) · 3.63 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import React, { useState, useEffect } from 'react';
import { Image, View, Text, Dimensions } from 'react-native';
import { Grid, Col, Row } from 'react-native-easy-grid';
import { Magnetometer } from 'expo-sensors';
const { height, width } = Dimensions.get('window');
export default App = () => {
const [subscription, setSubscription] = useState(null);
const [magnetometer, setMagnetometer] = useState(0);
useEffect(() => {
_toggle();
return () => {
_unsubscribe();
};
}, []);
const _toggle = () => {
if (subscription) {
_unsubscribe();
} else {
_subscribe();
}
};
const _subscribe = () => {
setSubscription(
Magnetometer.addListener((data) => {
setMagnetometer(_angle(data));
})
);
};
const _unsubscribe = () => {
subscription && subscription.remove();
subscription = null;
};
const _angle = (magnetometer) => {
if (magnetometer) {
let { x, y, z } = magnetometer;
if (Math.atan2(y, x) >= 0) {
angle = Math.atan2(y, x) * (180 / Math.PI);
}
else {
angle = (Math.atan2(y, x) + 2 * Math.PI) * (180 / Math.PI);
}
}
return Math.round(angle);
};
const _direction = (degree) => {
if (degree >= 22.5 && degree < 67.5) {
return 'NE';
}
else if (degree >= 67.5 && degree < 112.5) {
return 'E';
}
else if (degree >= 112.5 && degree < 157.5) {
return 'SE';
}
else if (degree >= 157.5 && degree < 202.5) {
return 'S';
}
else if (degree >= 202.5 && degree < 247.5) {
return 'SW';
}
else if (degree >= 247.5 && degree < 292.5) {
return 'W';
}
else if (degree >= 292.5 && degree < 337.5) {
return 'NW';
}
else {
return 'N';
}
};
// Match the device top with pointer 0° degree. (By default 0° starts from the right of the device.)
const _degree = (magnetometer) => {
return magnetometer - 90 >= 0 ? magnetometer - 90 : magnetometer + 271;
};
return (
<Grid style={{ backgroundColor: 'black' }}>
<Row style={{ alignItems: 'center' }} size={.9}>
<Col style={{ alignItems: 'center' }}>
<Text
style={{
color: '#fff',
fontSize: height / 26,
fontWeight: 'bold'
}}>
{_direction(_degree(magnetometer))}
</Text>
</Col>
</Row>
<Row style={{ alignItems: 'center' }} size={.1}>
<Col style={{ alignItems: 'center' }}>
<View style={{ position: 'absolute', width: width, alignItems: 'center', top: 0 }}>
<Image source={require('./assets/compass_pointer.png')} style={{
height: height / 26,
resizeMode: 'contain'
}} />
</View>
</Col>
</Row>
<Row style={{ alignItems: 'center' }} size={2}>
<Text style={{
color: '#fff',
fontSize: height / 27,
width: width,
position: 'absolute',
textAlign: 'center'
}}>
{_degree(magnetometer)}°
</Text>
<Col style={{ alignItems: 'center' }}>
<Image source={require("./assets/compass_bg.png")} style={{
height: width - 80,
justifyContent: 'center',
alignItems: 'center',
resizeMode: 'contain',
transform: [{ rotate: 360 - magnetometer + 'deg' }]
}} />
</Col>
</Row>
<Row style={{ alignItems: 'center' }} size={1}>
<Col style={{ alignItems: 'center' }}>
<Text style={{ color: '#fff' }}>Copyright @RahulHaque</Text>
</Col>
</Row>
</Grid>
);
}