在我的Android应用程序中,我需要使用GeoFireStore在一个范围内获得一个用户列表。
Firestore中的数据库结构:

我从这个链接得到的所有信息。
受抚养人:
implementation 'com.github.imperiumlabs:GeoFirestore-Android:v1.1.1'储存库:
maven { url 'https://jitpack.io' }A代码:
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() {
}发布于 2019-05-08 13:54:36
我添加这个作为一个答案,因为我不能添加评论。
所以,您要做的是在一定半径内获取所有的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);您可以找到正式文档这里。
作为最后一个技巧,我建议您将库更新为正确的版本(1.2.0),因为geo散列算法已经更改,您可以利用kotlin功能。
发布于 2019-05-08 08:07:41
所以,正如亚历克斯所说,我错过了数据库中的GeoHash。这是正确的。
将位置转换为散列的代码:
GeoHash geoHash = new GeoHash(32.9235234, 35.3310622);
String res = geoHash.getGeoHashString();结果:
res = svc57cx33m
但是,要使所有这些工作正常,您需要在Firestore中正确地存储数据。
如何在固定恢复 这里中正确地存储地理数据。
https://stackoverflow.com/questions/56005186
复制相似问题