forked from firebase/snippets-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.solution-geoqueries.js
103 lines (88 loc) · 2.74 KB
/
test.solution-geoqueries.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
import firebase from 'firebase/app';
import 'firebase/firestore';
const geofire = require('geofire-common');
/**
* @type firebase.firestore.Firestore
*/
var db;
function addHash(done) {
// [START fs_geo_add_hash]
// Compute the GeoHash for a lat/lng point
const lat = 51.5074;
const lng = 0.1278;
const hash = geofire.geohashForLocation([lat, lng]);
// Add the hash and the lat/lng to the document. We will use the hash
// for queries and the lat/lng for distance comparisons.
const londonRef = db.collection('cities').doc('LON');
londonRef.update({
geohash: hash,
lat: lat,
lng: lng
}).then(() => {
// [START_EXCLUDE]
done();
// [END_EXCLUDE]
});
// [END fs_geo_add_hash]
}
function queryHashes(done) {
// [START fs_geo_query_hashes]
// Find cities within 50km of London
const center = [51.5074, 0.1278];
const radiusInM = 50 * 1000;
// Each item in 'bounds' represents a startAt/endAt pair. We have to issue
// a separate query for each pair. There can be up to 9 pairs of bounds
// depending on overlap, but in most cases there are 4.
const bounds = geofire.geohashQueryBounds(center, radiusInM);
const promises = [];
for (const b of bounds) {
const q = db.collection('cities')
.orderBy('geohash')
.startAt(b[0])
.endAt(b[1]);
promises.push(q.get());
}
// Collect all the query results together into a single list
Promise.all(promises).then((snapshots) => {
const matchingDocs = [];
for (const snap of snapshots) {
for (const doc of snap.docs) {
const lat = doc.get('lat');
const lng = doc.get('lng');
// We have to filter out a few false positives due to GeoHash
// accuracy, but most will match
const distanceInKm = geofire.distanceBetween([lat, lng], center);
const distanceInM = distanceInKm * 1000;
if (distanceInM <= radiusInM) {
matchingDocs.push(doc);
}
}
}
return matchingDocs;
}).then((matchingDocs) => {
// Process the matching documents
// [START_EXCLUDE]
done(matchingDocs);
// [END_EXCLUDE]
});
// [END fs_geo_query_hashes]
}
describe("firestore-solution-geoqueries", () => {
before(() => {
var config = {
apiKey: "AIzaSyArvVh6VSdXicubcvIyuB-GZs8ua0m0DTI",
authDomain: "firestorequickstarts.firebaseapp.com",
projectId: "firestorequickstarts",
};
var app = firebase.initializeApp(config, "solution-geoqueries");
db = firebase.firestore(app);
});
describe("solution-geoqueries", () => {
it("should add a hash to a doc", (done) => {
addHash(done);
});
it("should query hashes", (done) => {
queryHashes(done);
});
});
});