java - 使用 GeoFireStore Android 从 Firestore 检索一定范围内的用户

标签 java android firebase google-cloud-firestore geofirestore

在我的 Android 应用程序中,我需要使用 GeoFireStoreFirestore 获取一定范围内的用户列表。

Firestore 中的数据库结构: enter image description here

我从这个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() {     
}

最佳答案

我将其添加为答案,因为我无法添加评论。

因此,您要做的就是获取一定半径内的所有 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 功能。

关于java - 使用 GeoFireStore Android 从 Firestore 检索一定范围内的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56005186/

相关文章:

java - Java,另一个方法内部的方法声明?

android - 更改对话框多选项目的颜色

javascript - 无法将 “custom blob” 保存到 Firestore - ADMIN SDK

java - IF条件语句格式错误?

java - 消息的自定义java序列化

java - Vaadin FormLayout 格式

Android 在 Canvas 上拖动两个图像

android - 使用 Google Earth Android Intent 飞向纬度/经度

firebase - 从react-native-firebase中删除admob sdk

java - Firebase Valueeventlistener 花费了很多时间