firebase - 使用 GeoFire 查询附近位置

标签 firebase geofire

我很难理解 GeoFire 如何查询附近的位置;

我正在构建一个基于地理的应用程序,它将根据用户位置获取附近的位置。我的数据结构如下

locations
    -Ke1uhoT3gpHR_VsehIv
    -Kdrel2Z_xWI280XNfGg
        -name: "place 1"
        -description: "desc 1"
geofire
    -Ke1uhoT3gpHR_VsehIv
    -Kdrel2Z_xWI280XNfGg
        -g: "dr5regw90s"
        -l
            -0: 40.7127837
            -1: -74.00594130000002

我似乎无法理解“tracking keys”以及离开和进入 GeoQueries 的位置的整个想法。 (也许这个概念与类似 Uber 的功能更相关)

假设上面的位置就在附近,我该如何使用自己的纬度和经度坐标来获取它们?我确信我误解了 GeoFires 文档,但我只是没有看到它。

最佳答案

要获取附近的所有位置, 首先,我们获取保存 GeoFire 位置的数据库引用。从你的问题来看,应该是

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("geofire");

接下来,我们使用 GeoFire 位置引用作为参数创建一个 GeoFire 实例

GeoFire geoFire = new GeoFire(ref);

现在我们使用它的 queryAtLocation 方法查询 GeoFire 引用,geoFire queryAtLoction 方法有 2 个参数:GeoLocation 对象和距离范围。因此,如果我们使用 3 作为距离范围,则任何距离用户 3 公里的位置都会显示在 onKeyEntered(...) 方法中。

注意:GeoLocation 对象有 2 个参数:纬度和经度。因此我们可以使用用户的纬度和经度作为参数。

        GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(userLatitde, userLongitude), 3);
        geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
            @Override
            public void onKeyEntered(String key, GeoLocation location) {
                //Any location key which is within 3km from the user's location will show up here as the key parameter in this method 
                //You can fetch the actual data for this location by creating another firebase query here
    Query locationDataQuery = new FirebaseDatabase.getInstance().child("locations").child(key);
    locationDataQuery..addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            //The dataSnapshot should hold the actual data about the location
    dataSnapshot.getChild("name").getValue(String.class); //should return the name of the location and dataSnapshot.getChild("description").getValue(String.class); //should return the description of the locations
                        }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }

            @Override
            public void onKeyExited(String key) {}

            @Override
            public void onKeyMoved(String key, GeoLocation location) {}

            @Override
            public void onGeoQueryReady() {
                //This method will be called when all the locations which are within 3km from the user's location has been loaded Now you can do what you wish with this data
            }

            @Override
            public void onGeoQueryError(DatabaseError error) {

            }
        });

关于firebase - 使用 GeoFire 查询附近位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42601587/

相关文章:

firebase - 错误 : Firebase Auth Google delete EmailPassword Auth

java - mChart android颜色没有改变

javascript - 如何获取初始 GeoFire 值

swift - 为什么 Firestore 有时不返回任何东西,没有错误,什么都没有?

javascript - Firebase getIdToken 不起作用

javascript - 重新连接到托管环境中的上一个 Puppeteer session

ios - 查找用户位置周围各种半径的点

swift - 使用 Geofire/Firebase 收集用户列表

arrays - 如何使用 Swift 使用 Firebase GeoFire 查询多个键?