sam
sam

Reputation: 228

how to check whether firebase user is present in 10km radius with geofire query

I want to check whether the firebase user is present in a 10km radius with Geofire query. if the user preset in that radius then show his post(add data to Recyclerview).

actually I am using this code, this code works fine but it rearranges the Recyclerview item positions according to distance and I don't want to change the positions of recycler items. I'm checking multiple user locations.

public void GetUserIDDetails() {

    ref.child("Post").child("Postdetails").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            if (dataSnapshot.exists()) {

                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                    ShopCategoryModel model = snapshot.getValue(ShopCategoryModel.class);

                    String UserID= model.getUserID();
                    String itemID = model.getItemID();

                    String expiredate = model.getExpiredate();

                    GetADNeatrby(UserID, itemID,10);
                }
            }
        }

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

public void GetADNeatrby(final String UserID, final String ItemID, int Radius) {

    GeoFire geoFire = new GeoFire(ref.child("buisinessID"));
    GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(latitude, longitude), Raius);

    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {

            if (key.equals(UserID)){
                getADdetails(UserID, ItemID);
            }                
        }

        @Override
        public void onKeyExited(String key) {
        }

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

        @Override
        public void onGeoQueryReady() {
        }

        @Override
        public void onGeoQueryError(DatabaseError error) {
        }
    });
}

public void getADdetails(final String UserID, final String ItemID) {

    userDataset.clear();

    ref.child("business").child(UserID).child("ADbanner").child(ItemID).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {


                ShopCategoryModel model = dataSnapshot.getValue(ShopCategoryModel.class);
                userDataset.add(model);

                NYOfferDataAdapter adapter = new NYOfferDataAdapter(NearBy_OfferSoffer.this.getContext(), userDataset);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        }

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

This code works properly but it has one problem that it rearranges the data according to the distance. But I want to show the data as it is stored data according to the time over the firebase database. Please help me out through this.

Upvotes: 0

Views: 244

Answers (1)

Nehal Godhasara
Nehal Godhasara

Reputation: 795

First you can collect key of all nearby users using geoQuery.

List<String> listNearBy=new ArrayList();
public void GetADNeatrby(final String UserID, final String ItemID, int Radius) {

    GeoFire geoFire = new GeoFire(ref.child("buisinessID"));
    GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(latitude, longitude), Raius);

    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {

listNearBy.add(key);
        }

        @Override
        public void onKeyExited(String key) {
        }

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

        @Override
        public void onGeoQueryReady() {
    GetUserIDDetails();
        }

        @Override
        public void onGeoQueryError(DatabaseError error) {
        }
    });
}

and in GetUserIDDetails() methods use only that data whose userID in collected key list listNearBy.

Hope this will help!!

Upvotes: 1

Related Questions