0

I have a firebase database(realtime database) with more than 700 objects. Each one of them has latitude and longitude. I would like to know if there is some way to query them and show only those which are near to the device location. I've tried to understand geofire but I couldn't. This is a example from one object in firebase: enter image description here and this is what i've been using to show all data:

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);
                }
            }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You're looking for Geofire, a library that encodes latitude and longitude into a single value and then allows querying on ranges of that. See https://stackoverflow.com/questions/37759727/search-in-firebase-based-on-distance/37765149#37765149 and https://stackoverflow.com/questions/40272146/firebase-location-query/40272342#40272342 – Frank van Puffelen Jun 01 '18 at 03:55

1 Answers1

0

yes, if you have a main tree node with all your usersID and inside each userID you have latitude and longitude, you can query all your users tree, and get inside of each one your latitude and longitude, and then you can do the logic to check if each pulled lat and long is nearby yours.

this is how you retrieve all the data, lets say you have a main tree node Users and inside that you have each userUID with latitude and longitude inside of each one, you should do something like this

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

then after you get all the values, just retrieve them from the arraylist and compare them to yours, do some logic there and you are done

I would suggest to you to store latitude and longitude in just 1 key and not separated, as it will be easier to fetch and use the value, then you can use regex to trim it out and get what you want from it

happy coding !

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77