如何使用Geo Queries获取接近传递 GeoLocation(double lat, double lng)
的所有位置 . 我有以下代码(它没有发生):
public void setCurrentLatLng(double lat, double lng){
this.lat = lat;
this.lng = lng;
GeoLocation geoLocation = new GeoLocation(lat, lng);
updateCurrenLocation(geoLocation);
GeoQuery geoQuery = geoFire.queryAtLocation(geoLocation, 8f);
geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
@Override
public void onDataEntered(DataSnapshot dataSnapshot, GeoLocation location) {
Log.d("geoQuery","onDataEntered "+dataSnapshot.toString());
// ...
}
@Override
public void onDataExited(DataSnapshot dataSnapshot) {
Log.d("geoQuery","onDataExited "+dataSnapshot.toString());
// ...
}
@Override
public void onDataMoved(DataSnapshot dataSnapshot, GeoLocation location) {
Log.d("geoQuery","onDataMoved "+dataSnapshot.toString());
// ...
}
@Override
public void onDataChanged(DataSnapshot dataSnapshot, GeoLocation location) {
Log.d("geoQuery","onDataChanged "+dataSnapshot.toString());
// ...
}
@Override
public void onGeoQueryReady() {
// ...
Log.d("geoQuery","onGeoQueryReady");
}
@Override
public void onGeoQueryError(DatabaseError error) {
Log.d("geoQuery","onGeoQueryError");
// ...
}
});
this.setChanged();
notifyObservers();
this.clearChanged();
Log.d("update","clearChanged");
}
这是我的火力基础数据:
我想我可以根据需要修改数据结构 .
日志
09-12 08:55:33.818 17710-17710/es.rchampa.weirdo D/geoQuery: lat=40.4430883 lng=-3.721805
09-12 08:55:33.982 17710-17710/es.rchampa.weirdo D/geoQuery: lat=40.4430883 lng=-3.721805
09-12 08:55:33.986 17710-17710/es.rchampa.weirdo D/geoQuery: onGeoQueryReady
09-12 08:55:34.025 17710-17710/es.rchampa.weirdo D/geoQuery: onGeoQueryReady
Gradle文件
....
// Firebase
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-storage:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.3'
implementation 'com.google.firebase:firebase-crash:16.2.0'
implementation 'com.google.firebase:firebase-core:16.0.3'
// Firebase UI
implementation 'com.firebaseui:firebase-ui-database:1.2.0'
//Firebase GeoFire
implementation 'com.firebase:geofire-android:2.3.1'
// Google Play Services
implementation 'com.google.android.gms:play-services-auth:16.0.0'
implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.android.gms:play-services-location:15.0.1'
....
更新
如果您愿意,我可以授予访问我的私人仓库的权限 .
3 回答
你传递值
8f
(浮动)为radius
,而radius
应该是8.0d
或Double.valueOf(8.0)
,其中MAX_SUPPORTED_RADIUS
等于8587
千米 .虽然实际问题是
GeoFire
已经需要知道.child("location")
,但是用Reference
代表它是不可能的 . 只有DataSnapshot
有getChildren()
.底线是:
所以这是一个工作
Java
的例子,因为......我们假设以下结构(如上所述):
数据库规则应该
.indexOn
为locations
fieldg
设置:模块中的依赖项
build.gradle
:这表明,如何通过
GeoQuery
结果获取用户的快照;注意
GeoQueryEventListener
而不是GeoQueryDataEventListener
:为了保持完整性,当删除用户记录时,需要删除关联的位置记录 - 否则会导致密钥无法再查找 .
问题是你根据问题传递半径https://github.com/firebase/geofire-java/issues/72
尝试传递这样的 Value 这可能会有所帮助
因为它现在正好
geofire
作为一个索引来打开地理查询,并提供你想要的文件的密钥(将存储在一个单独的"collection") .你应该使用
geofire
和一个单独的"collection"(称之为 usersLocations )现在您可以将它用作 users 的索引,并可以像这样添加项目 .
您的Firebase RTDB现在看起来像这样:
所以最后当你对你的_1470470进行查询时,你最终会解雇你拥有的任何听众 .
As a small note... I am not a Java developer, but I do use/know
geofire
in general. Hopefully my bits of advice/thoughts will be helpful.