-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
117 lines (96 loc) · 3.14 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
import { useState } from 'react';
import { StyleSheet, Button, Image, View } from 'react-native';
import * as ImagePicker from 'expo-image-picker';
import * as MediaLibrary from 'expo-media-library';
import * as Location from 'expo-location';
import MapView from 'react-native-maps';
const App = () => {
const [image, setImage] = useState(null);
const [location, setLocation] = useState(null);
// lets user pick an image / ask for permission first
const pickImage = async () => {
let permissions = await ImagePicker.requestMediaLibraryPermissionsAsync();
if (permissions?.granted) {
// default to pick is Images
// let result = await ImagePicker.launchImageLibraryAsync();
let result = await ImagePicker.launchImageLibraryAsync({
// Images, Videos or All
mediaTypes: ImagePicker.MediaTypeOptions.Images
});
if (!result.canceled) setImage(result.assets[0]);
else setImage(null)
}
};
// lets user take an image / ask for permission first
// persists photos on the device storage
const takePhoto = async () => {
let permissions = await ImagePicker.requestCameraPermissionsAsync();
if (permissions?.granted) {
let result = await ImagePicker.launchCameraAsync();
if (!result.canceled) {
let mediaLibraryPermissions = await MediaLibrary.requestPermissionsAsync();
if (mediaLibraryPermissions?.granted) await MediaLibrary.saveToLibraryAsync(result.assets[0].uri);
setImage(result.assets[0]);
} else setImage(null)
}
}
// lets user take an image / ask for permission first
// const takePhoto = async () => {
// let permissions = await ImagePicker.requestCameraPermissionsAsync();
// if (permissions?.granted) {
// let result = await ImagePicker.launchCameraAsync();
// if (!result.canceled) setImage(result.assets[0]);
// else setImage(null)
// }
// };
// requests and accesses user's geo location
const getLocation = async () => {
let permissions = await Location.requestForegroundPermissionsAsync();
if (permissions?.granted) {
const location = await Location.getCurrentPositionAsync({});
setLocation(location);
} else {
Alert.alert('No permission to read geo location.');
}
};
return (
<View style={styles.container}>
<Button
title='Pick an image from the library'
onPress={pickImage}
/>
<Button
title='Take a photo'
onPress={takePhoto}
/>
<Button
title='Get my location'
onPress={getLocation}
/>
{image &&
<Image
source={{ uri: image.uri }}
style={{ width: 200, height: 200 }}
/>
}
{location &&
<MapView
style={{ width: 300, height: 200 }}
region={{
latitude: location.coords.latitude,
longitude: location.coords.longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
/>}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#ecf0f1',
justifyContent: 'center',
}
});
export default App;