android – Geofire – 找不到位置

前端之家收集整理的这篇文章主要介绍了android – Geofire – 找不到位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何使用 Geo Queries获取接近传递的GeoLocation(double lat,double lng)的所有位置.
我有以下代码(它没有发生):
  1. public void setCurrentLatLng(double lat,double lng){
  2. this.lat = lat;
  3. this.lng = lng;
  4. GeoLocation geoLocation = new GeoLocation(lat,lng);
  5. updateCurrenLocation(geoLocation);
  6. GeoQuery geoQuery = geoFire.queryAtLocation(geoLocation,8f);
  7. geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
  8.  
  9. @Override
  10. public void onDataEntered(DataSnapshot dataSnapshot,GeoLocation location) {
  11. Log.d("geoQuery","onDataEntered "+dataSnapshot.toString());
  12. // ...
  13. }
  14.  
  15. @Override
  16. public void onDataExited(DataSnapshot dataSnapshot) {
  17. Log.d("geoQuery","onDataExited "+dataSnapshot.toString());
  18. // ...
  19. }
  20.  
  21. @Override
  22. public void onDataMoved(DataSnapshot dataSnapshot,"onDataMoved "+dataSnapshot.toString());
  23. // ...
  24. }
  25.  
  26. @Override
  27. public void onDataChanged(DataSnapshot dataSnapshot,"onDataChanged "+dataSnapshot.toString());
  28. // ...
  29. }
  30.  
  31. @Override
  32. public void onGeoQueryReady() {
  33. // ...
  34. Log.d("geoQuery","onGeoQueryReady");
  35. }
  36.  
  37. @Override
  38. public void onGeoQueryError(DatabaseError error) {
  39. Log.d("geoQuery","onGeoQueryError");
  40. // ...
  41. }
  42.  
  43. });
  44. this.setChanged();
  45. notifyObservers();
  46. this.clearChanged();
  47. Log.d("update","clearChanged");
  48. }

这是我的火力基础数据:

我想我可以根据需要修改数据结构.

日志

  1. 09-12 08:55:33.818 17710-17710/es.rchampa.weirdo D/geoQuery: lat=40.4430883 lng=-3.721805
  2. 09-12 08:55:33.982 17710-17710/es.rchampa.weirdo D/geoQuery: lat=40.4430883 lng=-3.721805
  3. 09-12 08:55:33.986 17710-17710/es.rchampa.weirdo D/geoQuery: onGeoQueryReady
  4. 09-12 08:55:34.025 17710-17710/es.rchampa.weirdo D/geoQuery: onGeoQueryReady

Gradle文件

  1. ....
  2. // Firebase
  3. implementation 'com.google.firebase:firebase-database:16.0.1'
  4. implementation 'com.google.firebase:firebase-storage:16.0.1'
  5. implementation 'com.google.firebase:firebase-auth:16.0.3'
  6. implementation 'com.google.firebase:firebase-crash:16.2.0'
  7. implementation 'com.google.firebase:firebase-core:16.0.3'
  8.  
  9. // Firebase UI
  10. implementation 'com.firebaseui:firebase-ui-database:1.2.0'
  11.  
  12. //Firebase GeoFire
  13. implementation 'com.firebase:geofire-android:2.3.1'
  14.  
  15. // Google Play Services
  16. implementation 'com.google.android.gms:play-services-auth:16.0.0'
  17. implementation 'com.google.android.gms:play-services-maps:15.0.1'
  18. implementation 'com.google.android.gms:play-services-location:15.0.1'
  19. ....

UPDATE

如果您愿意,我可以授予访问我的私人仓库的权限.

解决方法

你将值8f(float)作为半径传递,而半径应该是8.0d或Double.valueOf(8.0),其中MAX_SUPPORTED_RADIUS等于8587公里.

虽然实际问题是,GeoFire已经需要知道.child(“location”),但不可能用Reference来表示;只有DataSnapshot有getChildren().

底线是:

you’d have to create a separate locations Reference,in order to avoid the nesting. nevertheless you still can use the related uid key for these nodes (or at least add it as a child-node),in order to be able to look up within the users Reference. it’s a 1:1 relation,in between two References.

所以这是一个有效的Java示例,只是因为……

我们假设以下结构(如上所述):

  1. {
  2. "locations" : {
  3. "CR88aa9gnDfJYYGq5ZTMwwC38C12" : {
  4. ".priority" : "9q8yywdgue","g" : "9q8yywdgue","l" : [ 37.7853889,-122.4056973 ]
  5. }
  6. },"users" : {
  7. "CR88aa9gnDfJYYGq5ZTMwwC38C12" : {
  8. "displayName" : "user 01",...
  9. }
  10. }
  11. }

数据库规则应该有.indexOn用于位置字段g set:

  1. {
  2. "rules": {
  3. ...
  4. "locations": {
  5. ".indexOn": "g"
  6. }
  7. }
  8. }

模块的build.gradle中的依赖项:

  1. dependencies {
  2. ...
  3. api "com.firebase:geofire-android:2.3.1"
  4. }

这表明了如何通过GeoQuery结果获取用户的快照;

注意GeoQueryEventListener而不是GeoQueryDataEventListener:

  1. public class GeofireActivity extends AppCompatActivity {
  2.  
  3. private static final String LOG_TAG = GeofireActivity.class.getSimpleName();
  4.  
  5. private DatabaseReference refBase = null;
  6. private DatabaseReference refLocation = null;
  7. private DatabaseReference refUser = null;
  8.  
  9. private GeoFire geoFire = null;
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. this.setContentView(R.layout.fragment_geofire);
  15. this.setReferences();
  16. }
  17.  
  18. private void setReferences() {
  19. this.refBase = FirebaseDatabase.getInstance().getReference();
  20. this.refUser = refBase.child("users");
  21. this.refLocation = refBase.child("locations");
  22. this.geoFire = new GeoFire(this.refLocation);
  23. }
  24.  
  25. private void searchNearby(double latitude,double longitude,double radius) {
  26. this.searchNearby(new GeoLocation(latitude,longitude),radius);
  27. }
  28.  
  29. private void searchNearby(GeoLocation location,double radius) {
  30.  
  31. GeoQuery geoQuery = this.geoFire.queryAtLocation(location,radius);
  32. geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
  33.  
  34. @Override
  35. public void onKeyEntered(String key,GeoLocation location) {
  36.  
  37. String loc = String.valueOf(location.latitude) + "," + String.valueOf(location.longitude);
  38. Log.d(LOG_TAG,"onKeyEntered: " + key + " @ " + loc);
  39.  
  40. /* once the key is known,one can lookup the associated record */
  41. refUser.child(key).addListenerForSingleValueEvent(new ValueEventListener() {
  42.  
  43. @Override
  44. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  45. Log.d(LOG_TAG,"onDataChange: " + dataSnapshot.toString());
  46. }
  47.  
  48. @Override
  49. public void onCancelled(@NonNull DatabaseError firebaseError) {
  50. Log.e(LOG_TAG,"onCancelled: " + firebaseError.getMessage());
  51. }
  52. });
  53. }
  54.  
  55. @Override
  56. public void onKeyExited(String key) {
  57. Log.d(LOG_TAG,"onKeyExited: " + key);
  58. }
  59.  
  60. @Override
  61. public void onKeyMoved(String key,GeoLocation location) {
  62. Log.d(LOG_TAG,"onKeyMoved: " + key);
  63. }
  64.  
  65. @Override
  66. public void onGeoQueryReady() {
  67. Log.d(LOG_TAG,"onGeoQueryReady");
  68. }
  69.  
  70. @Override
  71. public void onGeoQueryError(DatabaseError error) {
  72. Log.e(LOG_TAG,"onGeoQueryError" + error.getMessage());
  73. }
  74. });
  75. }
  76. }

为了保持完整性,在删除用户记录时,需要删除关联的位置记录 – 否则会导致密钥无法再查找.

猜你在找的Android相关文章