简体   繁体   中英

I want to calculate user radius and retrieve the user id in android Firebase

I want that when user uploads posts so the notification should be sent to the user in 10 km radius. For this, I am calculating user radius by retrieving user latitude and longitude from firebase database and sending them to the distanceBetween function one by one along with the latitude and longitude of the post from where it is uploaded. But the problem is that which user comes in the radius.And I am unable to retrieve the user id and reg Token to send a notification.

Here is my code

dataref.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
                    @Override
                    public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {

                        for (com.google.firebase.database.DataSnapshot data : dataSnapshot.getChildren()) 
                            {
                             datasnap = data;
                                            }

Location.distanceBetween(Post_latitude, Post_longitude,Double.parseDouble(datasnap.child("latitude").getValue(String.class)), Double.parseDouble(datasnap.child("longitude").getValue(String.class)), results);
                        final double km = (results[0] / 1000);

                        if (km <= 10000) {
                            if (!String.valueOf(datasnap.getKey().toString()).equals(userID)) {

                //getting the user id here

                               Log.d("USerID", datasnap.getKey().toString() )
                            }
                        }

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

Here is my database image

I have found the solution of my problem. Use geofire to save and retrieve user location with in a specified radius.

here is the code:

// creates a new query around [37.7832, -122.4056] with a radius of 0.6 kilometers GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(37.7832, -122.4056), 0.6);

For details check the link: https://github.com/firebase/geofire-java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM