在我的Android应用程序中,我需要使用 GeoFireStore 在一定范围内从 Firestore 获取用户列表。
我从这link获得的所有信息。
依赖性:
implementation 'com.github.imperiumlabs:GeoFirestore-Android:v1.1.1'
存储库:
maven { url 'https://jitpack.io' }
代码:
CollectionReference geoFirestoreRef =
FirebaseFirestore.getInstance().collection("Users");
GeoFirestore geoFirestore = new GeoFirestore(geoFirestoreRef);
GeoQuery geoQuery = geoFirestore.queryAtLocation(new GeoPoint(32.848971, 35.0920935), 30);
geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
@Override
public void onDocumentEntered(DocumentSnapshot documentSnapshot, GeoPoint location) {
}
@Override
public void onDocumentExited(DocumentSnapshot documentSnapshot) {
}
@Override
public void onDocumentMoved(DocumentSnapshot documentSnapshot, GeoPoint location) {
}
@Override
public void onDocumentChanged(DocumentSnapshot documentSnapshot, GeoPoint location) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(Exception exception) {
}
});
所以我没有做我不能调用一个函数:
@Override
public void onDocumentEntered(DocumentSnapshot documentSnapshot, GeoPoint location) {
}
仅此函数称为:
@Override
public void onGeoQueryReady() {
}
答案 0 :(得分:1)
我将其添加为答案,因为我无法添加评论。
因此,您想要做的是将所有DocumentSnapshots
置于一定的半径内,为此您的代码正确,您可以使用GeoFirestore
创建一个新的CollectionReference
实例,然后查询所需的位置。
问题在于,您的数据库中当前没有没有位置数据可供GeoFirestore使用;您可以通过在将文档添加到数据库中时添加“位置数据”来解决此问题。
implementation 'com.github.imperiumlabs:GeoFirestore-Android:v1.2.0'
/*
* You have to do this only one time for document
* so the best is after the document upload to Firestore.
*/
//The id of the document you want to add the location
String documentId = documentReference.id;
GeoPoint point = new GeoPoint(/*lat,long of your point*/);
geoFirestore.setLocation(documentId, point);
您可以找到官方文档here。
作为最后一个技巧,我建议您将库更新为正确的版本(1.2.0),因为更改了地理哈希算法,并且可以利用kotlin功能。
答案 1 :(得分:0)
因此,正如亚历克斯所说,我错过了数据库中的GeoHash。这是正确的。
代码将位置转换为哈希:
GeoHash geoHash = new GeoHash(32.9235234, 35.3310622);
String res = geoHash.getGeoHashString();
结果:
res = svc57cx33m
但是,要使所有这些正常工作,您需要将数据正确存储在 Firestore 中。
如何在 Firestore here中正确存储地理数据。