Ric*_*rdo 8 android firebase geofire firebase-realtime-database
如何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");
}
Run Code Online (Sandbox Code Playgroud)
我想我可以根据需要修改数据结构.
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
Run Code Online (Sandbox Code Playgroud)
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'
....
Run Code Online (Sandbox Code Playgroud)
如果您愿意,我可以授予访问我的私人仓库的权限.
您将 value 8f
(float) 作为 the传递radius
,而 theradius
应该是8.0d
或Double.valueOf(8.0)
,其中MAX_SUPPORTED_RADIUS
等于8587
公里。
虽然实际问题是,GeoFire
已经需要知道.child("location")
,但不可能用 ; 来表示Reference
它 只有.DataSnapshot
getChildren()
底线是:
您必须创建一个单独的位置
Reference
,以避免嵌套。尽管如此,您仍然可以使用这些节点的相关uid
键(或者至少将其添加为子节点),以便能够在用户中查找Reference
。这是1:1
两个 s 之间的关系Reference
。
所以这是一个有效的Java
例子,只是因为......
我们假设以下结构(如上所述):
{
"locations" : {
"CR88aa9gnDfJYYGq5ZTMwwC38C12" : {
".priority" : "9q8yywdgue",
"g" : "9q8yywdgue",
"l" : [ 37.7853889, -122.4056973 ]
}
},
"users" : {
"CR88aa9gnDfJYYGq5ZTMwwC38C12" : {
"displayName" : "user 01",
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
数据库规则应具有.indexOn
字段locations
集g
:
{
"rules": {
...
"locations": {
".indexOn": "g"
}
}
}
Run Code Online (Sandbox Code Playgroud)
模块中的依赖项build.gradle
:
dependencies {
...
implementation "com.firebase:geofire-android:2.3.1"
}
Run Code Online (Sandbox Code Playgroud)
这演示了如何通过GeoQuery
结果获取用户的快照;
请注意,GeoQueryEventListener
而不是GeoQueryDataEventListener
:
public class GeofireActivity extends AppCompatActivity {
private static final String LOG_TAG = GeofireActivity.class.getSimpleName();
private DatabaseReference refBase = null;
private DatabaseReference refLocation = null;
private DatabaseReference refUser = null;
private GeoFire geoFire = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.fragment_geofire);
this.setReferences();
}
private void setReferences() {
this.refBase = FirebaseDatabase.getInstance().getReference();
this.refUser = refBase.child("users");
this.refLocation = refBase.child("locations");
this.geoFire = new GeoFire(this.refLocation);
}
private void searchNearby(double latitude, double longitude, double radius) {
this.searchNearby(new GeoLocation(latitude, longitude), radius);
}
private void searchNearby(GeoLocation location, double radius) {
GeoQuery geoQuery = this.geoFire.queryAtLocation(location, radius);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
String loc = String.valueOf(location.latitude) + ", " + String.valueOf(location.longitude);
Log.d(LOG_TAG, "onKeyEntered: " + key + " @ " + loc);
/* once the key is known, one can lookup the associated record */
refUser.child(key).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.d(LOG_TAG, "onDataChange: " + dataSnapshot.toString());
}
@Override
public void onCancelled(@NonNull DatabaseError firebaseError) {
Log.e(LOG_TAG, "onCancelled: " + firebaseError.getMessage());
}
});
}
@Override
public void onKeyExited(String key) {
Log.d(LOG_TAG, "onKeyExited: " + key);
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
Log.d(LOG_TAG, "onKeyMoved: " + key);
}
@Override
public void onGeoQueryReady() {
Log.d(LOG_TAG, "onGeoQueryReady");
}
@Override
public void onGeoQueryError(DatabaseError error) {
Log.e(LOG_TAG, "onGeoQueryError" + error.getMessage());
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
为了保持完整性,当删除用户记录时,需要删除关联的位置记录 - 否则将导致无法再查找密钥。
归档时间: |
|
查看次数: |
618 次 |
最近记录: |