forked from firebase/snippets-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.solution-geoqueries.js
103 lines (83 loc) · 2.82 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
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]
const { Firestore } = require('firebase/firestore');
const geofire = require('geofire-common');
/** @type {Firestore} */
let db;
async function addHash(done) {
// [START fs_geo_add_hash]
const { doc, updateDoc } = require('firebase/firestore');
// 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 = doc(db, 'cities', 'LON');
await updateDoc(londonRef, {
geohash: hash,
lat: lat,
lng: lng
});
// [END fs_geo_add_hash]
done();
}
async function queryHashes(done) {
// [START fs_geo_query_hashes]
const { collection, query, orderBy, startAt, endAt, getDocs } = require('firebase/firestore');
// 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 = query(
collection(db, 'cities'),
orderBy('geohash'),
startAt(b[0]),
endAt(b[1]));
promises.push(getDocs(q));
}
// Collect all the query results together into a single list
const snapshots = await Promise.all(promises);
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);
}
}
}
// [END fs_geo_query_hashes]
done(matchingDocs);
}
describe("firestore-solution-geoqueries", () => {
before(() => {
const { initializeApp } = require("firebase/app");
const { getFirestore} = require("firebase/firestore");
const config = {
apiKey: "AIzaSyArvVh6VSdXicubcvIyuB-GZs8ua0m0DTI",
authDomain: "firestorequickstarts.firebaseapp.com",
projectId: "firestorequickstarts",
};
const app = initializeApp(config, "solution-geoqueries");
db = getFirestore(app);
});
describe("solution-geoqueries", () => {
it("should add a hash to a doc", (done) => {
addHash(done);
});
it("should query hashes", (done) => {
queryHashes(done);
});
});
});