-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
215 lines (191 loc) · 5.57 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import React, {useEffect, useState, useMemo, useRef, useCallback} from 'react';
import {StyleSheet, Platform} from 'react-native';
import {
ViroNode,
ViroFlexView,
ViroARScene,
ViroText,
ViroQuad,
ViroBox,
ViroTrackingStateConstants,
ViroARSceneNavigator,
} from '@viro-community/react-viro';
import CompassHeading from 'react-native-compass-heading';
import Geolocation from '@react-native-community/geolocation';
import haversine from 'haversine';
import {requestMultiple, PERMISSIONS, RESULTS} from 'react-native-permissions';
const qiblaPosition = {
latitude: 21.4224779,
longitude: 39.8251832,
};
const latLongToMerc = (latDeg, longDeg) => {
// From: https://gist.github.com/scaraveos/5409402
const longRad = (longDeg / 180.0) * Math.PI;
const latRad = (latDeg / 180.0) * Math.PI;
const smA = 6378137.0;
const xmeters = smA * longRad;
const ymeters = smA * Math.log((Math.sin(latRad) + 1) / Math.cos(latRad));
return {x: xmeters, y: ymeters};
};
const HelloWorldSceneAR = () => {
const [text, setText] = useState('Initializing AR...');
const [currentPosition, setCurrentPosition] = useState();
const [cameraPermission, setCameraPermission] = useState();
const [locationPermission, setLocationPermission] = useState();
const [compassHeading, setCompassHeading] = useState(0);
const locationRef = useRef();
function onInitialized(state, reason) {
if (state === ViroTrackingStateConstants.TRACKING_NORMAL) {
setText('Hello World!');
} else if (state === ViroTrackingStateConstants.TRACKING_NONE) {
// Handle loss of tracking
}
}
const distance = useMemo(() => {
if (!currentPosition) {
return 0;
}
return haversine(currentPosition, qiblaPosition);
}, [currentPosition]);
useEffect(() => {
// Permission.
const permissions = Platform.select({
ios: [PERMISSIONS.IOS.CAMERA, PERMISSIONS.IOS.LOCATION_WHEN_IN_USE],
android: [
PERMISSIONS.ANDROID.CAMERA,
PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
],
});
requestMultiple(permissions).then(statuses => {
if (Platform.OS == 'ios') {
setLocationPermission(
statuses[PERMISSIONS.IOS.LOCATION_WHEN_IN_USE] === RESULTS.GRANTED,
);
setCameraPermission(
statuses[PERMISSIONS.IOS.CAMERA] === RESULTS.GRANTED,
);
} else {
setLocationPermission(
statuses[PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION] ===
RESULTS.GRANTED,
);
setCameraPermission(
statuses[PERMISSIONS.ANDROID.CAMERA] === RESULTS.GRANTED,
);
}
});
// Compass.
CompassHeading.start(3, ({heading}) => {
setCompassHeading(heading);
});
return () => {
CompassHeading.stop();
};
}, []);
useEffect(() => {
if (locationPermission) {
Geolocation.watchPosition(
data => {
setCurrentPosition({
latitude: data.coords.latitude,
longitude: data.coords.longitude,
});
},
error => {},
{distanceFilter: 10},
);
}
return () => {
if (locationRef.current) {
Geolocation.clearWatch(locationRef.current);
}
};
}, [locationPermission]);
const arCoords = useMemo(() => {
if (!currentPosition) {
return {x: 0, z: 0};
}
const deviceObjPoint = latLongToMerc(
qiblaPosition.latitude,
qiblaPosition.longitude,
);
const mobilePoint = latLongToMerc(
currentPosition.latitude,
currentPosition.longitude,
);
const objDeltaY = deviceObjPoint.y - mobilePoint.y;
const objDeltaX = deviceObjPoint.x - mobilePoint.x;
if (Platform.OS === 'android') {
let angleRadian = (0 * Math.PI) / 180;
let newObjX =
objDeltaX * Math.cos(angleRadian) - objDeltaY * Math.sin(angleRadian);
let newObjY =
objDeltaX * Math.sin(angleRadian) + objDeltaY * Math.cos(angleRadian);
return {x: newObjX, z: -newObjY};
}
return {x: objDeltaX, z: -objDeltaY};
}, [currentPosition]);
const scale = useMemo(() => {
return Math.abs(Math.round(arCoords.z / 2));
}, [arCoords]);
const placeArObject = useCallback(
() => (
<ViroNode
scale={[scale, scale, scale]}
rotation={[0, 0, 0]}
position={[arCoords.x, 0, arCoords.z]}>
<ViroFlexView
style={{alignItems: 'center', justifyContent: 'center'}}
transformBehaviors={['billboard']}>
<ViroText
width={4}
height={0.5}
text="Qibla"
style={styles.textStyle}
/>
<ViroText
width={4}
height={0.5}
text={`${Math.round(distance)} km`}
style={styles.textStyle}
position={[0, -0.75, 0]}
/>
<ViroQuad width={1} height={1} position={[0, 0, 30]} />
</ViroFlexView>
</ViroNode>
),
[arCoords, distance],
);
return (
<ViroARScene onTrackingUpdated={onInitialized}>
{currentPosition && placeArObject()}
{/* <ViroText
text={`${Math.round(distance)}`}
scale={[0.5, 0.5, 0.5]}
position={[0, 0, -1]}
style={styles.helloWorldTextStyle}
/> */}
</ViroARScene>
);
};
export default () => {
return (
<ViroARSceneNavigator
autofocus={true}
initialScene={{
scene: HelloWorldSceneAR,
}}
style={styles.f1}
/>
);
};
var styles = StyleSheet.create({
f1: {flex: 1},
textStyle: {
fontFamily: 'Arial',
fontSize: 30,
color: '#ffffff',
textAlignVertical: 'center',
textAlign: 'center',
},
});