Android:从firebase查询附近的位置

时间:2018-05-31 19:24:34

标签: android firebase firebase-realtime-database

我有一个包含700多个对象的firebase数据库(实时数据库)。他们每个人都有经纬度。我想知道是否有某种方式来查询它们并只显示那些靠近设备位置的方法。我试过了解geofire,但我不能。 这是firebase中一个对象的示例: enter image description here 这就是我用来显示所有数据的原因:

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

1 个答案:

答案 0 :(得分:0)

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

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

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

然后在获得所有值之后,只需从arraylist中检索它们并将它们与你的相比较,在那里做一些逻辑并且你已经完成了

我建议你只用1个键存储纬度和经度而不是分开,因为它更容易获取并使用该值,然后你可以使用正则表达式来修剪它并从中得到你想要的东西< / p>

快乐的编码!

相关问题