按照教程
當我查詢任何位置時,我總是得到 0 結果,然后我嘗試查詢非嵌套集合(如教程視頻所示
嵌套集合的代碼(這是我的要求并且不起作用)
public void queryHashes() {
// Find cities within 50km of London
final GeoLocation center = new GeoLocation(19.912524, 76.079626);
final double 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.
List<GeoQueryBounds> bounds = GeoFireUtils.getGeoHashQueryBounds(center, radiusInM);
final List<Task<QuerySnapshot>> tasks = new ArrayList<>();
for (GeoQueryBounds b : bounds) {
Query q = db.collection("Owners").document().collection("Ghars")
.orderBy("geohash")
.startAt(b.startHash)
.endAt(b.endHash);
tasks.add(q.get());
}
// Collect all the query results together into a single list
Tasks.whenAllComplete(tasks)
.addOnCompleteListener(new OnCompleteListener<List<Task<?>>>() {
@Override
public void onComplete(@NonNull Task<List<Task<?>>> t) {
List<DocumentSnapshot> matchingDocs = new ArrayList<>();
for (Task<QuerySnapshot> task : tasks) {
QuerySnapshot snap = task.getResult();
for (DocumentSnapshot doc : snap.getDocuments()) {
double lat = doc.getDouble("lat");
double lng = doc.getDouble("lng");
// We have to filter out a few false positives due to GeoHash
// accuracy, but most will match
GeoLocation docLocation = new GeoLocation(lat, lng);
double distanceInM = GeoFireUtils.getDistanceBetween(docLocation, center);
if (distanceInM <= radiusInM) {
matchingDocs.add(doc);
}
}
}
// matchingDocs contains the results
// ...
Log.v("matchingDocsD", matchingDocs.size() "");
}
});
}
非嵌套集合的代碼(有效)
public void queryHashes() {
// Find cities within 50km of London
final GeoLocation center = new GeoLocation(19.912524, 76.079626);
final double 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.
List<GeoQueryBounds> bounds = GeoFireUtils.getGeoHashQueryBounds(center, radiusInM);
final List<Task<QuerySnapshot>> tasks = new ArrayList<>();
for (GeoQueryBounds b : bounds) {
Query q = db.collection("10K")
.orderBy("geohash")
.startAt(b.startHash)
.endAt(b.endHash);
tasks.add(q.get());
}
// Collect all the query results together into a single list
Tasks.whenAllComplete(tasks)
.addOnCompleteListener(new OnCompleteListener<List<Task<?>>>() {
@Override
public void onComplete(@NonNull Task<List<Task<?>>> t) {
List<DocumentSnapshot> matchingDocs = new ArrayList<>();
for (Task<QuerySnapshot> task : tasks) {
QuerySnapshot snap = task.getResult();
for (DocumentSnapshot doc : snap.getDocuments()) {
double lat = doc.getDouble("lat");
double lng = doc.getDouble("lng");
// We have to filter out a few false positives due to GeoHash
// accuracy, but most will match
GeoLocation docLocation = new GeoLocation(lat, lng);
double distanceInM = GeoFireUtils.getDistanceBetween(docLocation, center);
if (distanceInM <= radiusInM) {
matchingDocs.add(doc);
}
}
}
// matchingDocs contains the results
// ...
Log.v("matchingDocsD", matchingDocs.size() "");
}
});
}
uj5u.com熱心網友回復:
此查詢永遠不會回傳任何結果:
Query q = db.collection("Owners").document().collection("Ghars")
.orderBy("geohash")
.startAt(b.startHash)
.endAt(b.endHash);
當您在document()沒有任何引數的情況下呼叫時,您正在創建對一個新的、不存在的檔案的參考。所以它的Ghars子集合也不存在,你的查詢沒有結果。
如果要查詢特定所有者的Ghars子集合,則應在呼叫中指定所有者的檔案 ID document():
// ??
Query q = db.collection("Owners").document(ownerDocId).collection("Ghars")
.orderBy("geohash")
.startAt(b.startHash)
.endAt(b.endHash);
如果要查詢所有Ghars子集合,可以使用集合組查詢,它查詢具有特定名稱的所有集合:
// ??
Query q = db.collectionGroup("Ghars")
.orderBy("geohash")
.startAt(b.startHash)
.endAt(b.endHash);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/409505.html
標籤:
