【问题标题】:Retrieve users from Firestore within a range using GeoFireStore Android使用 GeoFireStore Android 从 Firestore 检索用户
【发布时间】:2019-05-06 12:24:17
【问题描述】:

在我的 Android 应用程序中,我需要使用 GeoFireStore 获取范围内来自 Firestore 的用户列表。

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() {     
}

【问题讨论】:

  • 您在使用此代码时发生了什么?
  • 发生调用函数onGeoQueryReady()
  • 不,在错误方面发生了什么。使用上述代码时的行为是什么?
  • 我没有从 Firestore 收到既定范围内的用户列表。 (半径)
  • 你确定你有正确的坐标并且用户在这个范围内吗?还请添加您的数据库结构作为屏幕截图。

标签: java android firebase google-cloud-firestore geofirestore


【解决方案1】:

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

因此,您要做的是在某个半径内获取所有 DocumentSnapshots,为此您的代码是正确的,您使用 CollectionReference 创建一个新的 GeoFirestore 实例,然后查询你想要的位置。

问题是在您的数据库中目前没有可供 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 功能。

【讨论】:

    【解决方案2】:

    所以,正如 Alex 所说,我错过了数据库中的 GeoHash。这是正确的。

    将位置转换为哈希的代码:

    GeoHash geoHash = new GeoHash(32.9235234, 35.3310622);
    String res = geoHash.getGeoHashString();
    

    结果:

    res = svc57cx33m

    但要使所有这些工作,您需要将数据正确存储在 Firestore 中。

    如何在 Firestore here 中正确存储地理数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      相关资源
      最近更新 更多