-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
149 lines (113 loc) · 3.28 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
import React from 'react';
import { StyleSheet, Text, View, TouchableOpacity, Alert, ScrollView } from 'react-native';
import firebase from './config/firebase';
import { GeoFirestore } from 'geofirestore';
// Introducir varios puntos
const lat = 37.462936;
const lng = -5.964594;
// Center
const centerLat = 37.394198;
const centerLng = -6.000574;
const radius = 200;
export default class App extends React.Component {
state = {
users: []
}
async createDocument() {
const doc = {
name: 'name',
age: 'age',
coordinates: new firebase.firestore.GeoPoint(lat, lng),
};
const geofirestore = new GeoFirestore(firebase.firestore());
const geocollection = geofirestore.collection('users');
await geocollection.add(doc).then( async (docRef) => {
console.log(docRef);
});
}
readDocument() {
const firestore = firebase.firestore();
const geofirestore = new GeoFirestore(firestore);
const geocollection = geofirestore.collection('users');
geocollection.limit(50).near({
center: new firebase.firestore.GeoPoint(centerLat, centerLng),
radius: radius
}).get().then((querySnapshot) => {
let users = [];
for (let i = 0; i < querySnapshot.docs.length; i++) {
let {doc} = querySnapshot.docChanges()[i];
//console.log(doc.distance)
let user = {...querySnapshot.docs[i].data(), distance: doc.distance, id: querySnapshot.docs[i].id}
users.push(user)
//Alert.alert(JSON.stringify(users))
}
console.log(users)
this.setState({users: users})
});
}
// If there is any change in any document in the user collection, it breaks
async readListenDocument() {
const firestore = firebase.firestore();
const geofirestore = new GeoFirestore(firestore);
const geocollection = geofirestore.collection('users');
let query = await geocollection.limit(50).near({
center: new firebase.firestore.GeoPoint(centerLat, centerLng),
radius: radius
});
let docQuery = await query.onSnapshot((snapshot) => {
//console.log('snapshot', snapshot)
let users = [];
for (let i = 0; i < snapshot.docs.length; i++) {
let {doc} = snapshot.docChanges()[i];
//console.log(doc.distance)
let user = {...snapshot.docs[i].data(), distance: doc.distance, id: snapshot.docs[i].id}
users.push(user)
//Alert.alert(JSON.stringify(users))
}
console.log(users)
this.setState({users: users})
//console.log('doc changes', snapshot.docChanges())
});
}
componentDidMount() {
//console.log(firebase)
}
render() {
//console.log('state', this.state.users);
return (
<ScrollView>
<View style={styles.container}>
<TouchableOpacity onPress={() => this.createDocument()}>
<Text>
Add
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => this.readListenDocument()}>
<Text>
query
</Text>
</TouchableOpacity>
{this.state ? this.state.users.map(user => {
return (
<View key={user.id}>
<Text>id: {user.id}</Text>
<Text>distance: {user.distance}</Text>
</View>
)
})
:
null}
</View>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
paddingTop: 20
},
});