代码之家  ›  专栏  ›  技术社区  ›  Ricardo

Geofire-未找到位置

  •  8
  • Ricardo  · 技术社区  · 7 年前

    GeoLocation(double lat, double lng) 使用 Geo Queries .

    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");
    }
    

    这是我的firebase数据: enter image description here

    如果需要的话,我可以修改数据结构。

    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
    

    渐变文件

    ....
    // 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 回复  |  直到 7 年前
        1
  •  3
  •   Martin Zeitler    7 年前

    你传递值 8f radius 在那里,当 应该是 8.0d Double.valueOf(8.0) ,在哪里 MAX_SUPPORTED_RADIUS 等于 8587 公里。

    但真正的问题是 GeoFire .child("location") ,但不可能用 Reference ;仅限 DataSnapshot getChildren() .

    你必须创建一个单独的 位置 参考 ,以避免嵌套。尽管如此,你仍然可以使用相关的 uid 参考 1:1 两者之间的关系 参考 s。

    所以这里有一个工作 Java 举个例子,就因为。。。

    {
      "locations" : {
        "CR88aa9gnDfJYYGq5ZTMwwC38C12" : {
          ".priority" : "9q8yywdgue",
          "g" : "9q8yywdgue",
          "l" : [ 37.7853889, -122.4056973 ]
        }
      },
      "users" : {
        "CR88aa9gnDfJYYGq5ZTMwwC38C12" : {
          "displayName" : "user 01",
          ...
        }
      }
    }
    

    .indexOn 对于 locations g 设置:

    {
      "rules": {
        ...
        "locations": {
          ".indexOn": "g"
        }
      }
    }
    

    build.gradle

    dependencies {
        ...
        implementation "com.firebase:geofire-android:2.3.1"
    }
    

    这演示了如何通过 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());
                }
            });
        }
    }
    

        2
  •  2
  •   Ashvin solanki    7 年前

    问题是当你通过半径时 根据问题 https://github.com/firebase/geofire-java/issues/72

        double radius = 8589; // Fails
    //  double radius = 8587.8; //Passes
    

    像这样传递值可能会有所帮助

    //GeoQuery geoQuery = geoFire.queryAtLocation(geoLocation, 8f);
    GeoQuery geoQuery = geoFire.queryAtLocation(geoLocation, radius);
    

    传递值8f(float)作为半径,而 最大支持半径等于8587公里。

        3
  •  2
  •   MichaelSolati    7 年前

    就目前的情况来看 geofire sort可以作为索引对其进行地理查询,并提供所需文档的键(该键将存储在单独的“集合”中)。

    你应该使用 以及一个单独的“集合”(称之为 用户位置 )

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("usersLocations");
    GeoFire geoFire = new GeoFire(ref);
    

    现在您可以将其用作 用户 ,并可以像这样向其中添加项。

    geoFire.setLocation('QymlMpC0Zc', new GeoLocation(40.2334983, -3.7185183));
    

    您的Firebase RTDB现在将如下所示:

    {
       'users': {
            'QymlMpC0Zc': {
                // All your data here
            }
        },
       'usersLocations': {
            'QymlMpC0Zc': {
                'g': 'ezjkgkk305',
                'l': {
                    '0': 40.2334983,
                    '1': -3.7185183
                }
            }
        }
    }
    

    所以最后当你对你的 geoFire

    作为一个小提示。。。我不是Java开发人员,但我确实使用/知道 地火 一般来说。希望我的一些建议/想法会有所帮助。