【问题标题】:Android: Query nearby locations from firebaseAndroid:从firebase查询附近的位置
【发布时间】:2018-05-31 19:24:33
【问题描述】:

我有一个包含 700 多个对象的 firebase 数据库(实时数据库)。他们每个人都有纬度和经度。我想知道是否有某种方法可以查询它们并仅显示靠近设备位置的那些。我试图理解 geofire,但我做不到。 这是 firebase 中一个对象的示例: 这就是我用来显示所有数据的方法:

ChildEventListener childEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
                mDatabase = FirebaseDatabase.getInstance().getReference();
                Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());

                //Get firebase info for each gym
                Gym gym=dataSnapshot.getValue(Gym.class);
                gym.setGym_id(dataSnapshot.getKey());


               //Set custom marker with firebase info
                LatLng newLocation= new LatLng(Double.parseDouble(gym.getLatitude()),Double.parseDouble(gym.getLongitude()));
                if (dataSnapshot.hasChild("raid")){
                    Marker marker= mMap.addMarker(new MarkerOptions()
                            .position(newLocation)
                            .title(gym.getName())
                            .snippet(gym.getUrl())
                            .icon(BitmapDescriptorFactory.fromBitmap(bMapRaid)));
                    marker.setTag(gym);
                } else {
                    Marker marker= mMap.addMarker(new MarkerOptions()
                            .position(newLocation)
                            .title(gym.getName())
                            .snippet(gym.getUrl())
                            .icon(BitmapDescriptorFactory.fromBitmap(bMap)));
                    marker.setTag(gym);
                }
            }

【问题讨论】:

标签: android firebase firebase-realtime-database


【解决方案1】:

是的,如果您有一个包含所有用户 ID 的主树节点,并且在每个用户 ID 内都有纬度和经度,您可以查询所有用户树,并在每个用户树中获取您的纬度和经度,然后您可以检查每个拉出的经纬度是否在您附近的逻辑。

这就是你检索所有数据的方式,假设你有一个主树节点用户,并且在其中你有每个用户UID,每个用户UID都有纬度和经度,你应该做这样的事情

  mDatabase.child("Users").addValueEventListener( new ValueEventListener(){
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ArrayList<Users> users = new Arraylist<>(); //an arraylist to save all the latitude and longitude values
            for(DataSnapshot post : dataSnapshot.getChildren() ){
                // Iterate through all your users and get latitude and longitude
               Users u = users.getValue(Users.class); //here you need a pojo in which you will have the variables you want to fetch, they need to be named exactly as your database one , remember, a pojo is just a class with setters and getters
               //and then just get your values and add them to your array
              long latlang =  u.latitudelongitude; //store your latlang
// in your case you dont have 1 key with latitude and longitude, you will have to fetch them separate, just do long latitude = u.latitude; and long longitude = u.longitude;
               users.add(latlang);  //add it to your arraylist

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });

然后在你得到所有值之后,从数组列表中检索它们并将它们与你的比较,在那里做一些逻辑,你就完成了

我建议您将纬度和经度仅存储在 1 个键中而不是分开,因为这样更容易获取和使用该值,然后您可以使用正则表达式对其进行修剪并从中获取您想要的内容

编码愉快!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-07
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 2017-06-20
    相关资源
    最近更新 更多