-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocationPermissionHelpers.js
88 lines (86 loc) · 2.1 KB
/
locationPermissionHelpers.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 {Platform} from 'react-native';
export function checkLocationPermission() {
return new Promise(function(resolve, reject) {
const {
PERMISSIONS: {ANDROID, IOS},
check,
RESULTS,
} = require('react-native-permissions');
check(
Platform.select({
android: ANDROID.ACCESS_FINE_LOCATION,
ios: IOS.LOCATION_WHEN_IN_USE,
}),
)
.then(result => {
switch (result) {
case RESULTS.UNAVAILABLE:
reject(result);
break;
case RESULTS.DENIED:
reject(result);
break;
case RESULTS.GRANTED:
resolve(result);
break;
case RESULTS.BLOCKED:
reject(result);
break;
}
})
.catch(e => {
reject(e);
});
});
}
export function requestLocationPermission() {
return new Promise(function(resolve, reject) {
const {
PERMISSIONS: {ANDROID, IOS},
RESULTS,
request,
} = require('react-native-permissions');
request(
Platform.select({
android: ANDROID.ACCESS_FINE_LOCATION,
ios: IOS.LOCATION_WHEN_IN_USE,
}),
).then(result => {
switch (result) {
case RESULTS.UNAVAILABLE:
reject(result);
break;
case RESULTS.DENIED:
reject(result);
break;
case RESULTS.GRANTED:
resolve(result);
break;
case RESULTS.BLOCKED:
break;
}
});
});
}
export function getUserLocation() {
return new Promise(function(resolve, reject) {
const Geolocation = require('react-native-geolocation-service').default;
Geolocation.getCurrentPosition(
position => {
// const {
// coords: {latitude, longitude},
// } = position;
//
resolve(position.coords);
},
error => {
// See error code charts below.
if (__DEV__) {
console.log('log:: Geolocation', error);
}
reject(error);
},
{enableHighAccuracy: true, timeout: 5000, maximumAge: 10000},
);
});
}