Storing Extra Data Under GeoFire Nodes

后端 未结 2 1259
予麋鹿
予麋鹿 2020-12-06 14:23

I am storing location data via GeoFire in my database, here is the structure:

geofire
  -Ke1uhoT3gpHR_VsehIv
  -Kdrel2Z_xWI280XNfGg
    -g: \"dr5regw90s\"
           


        
2条回答
  •  悲哀的现实
    2020-12-06 15:10

    It might be late but just in case someone else is facing the same issue.

    It's very possible to integrate and manipulate GeoFire data with existing data. The main challenge faced by many developers is the fact that they can't update the GeoFire geolocation without deleting the existing data using geoFire.setLocation("id", location); as reported in this issue geofire-java/issues/134.

    This is my solution for that.

    1. Setting mixed data with GeoFire on FireBase

    Let's consider below Structure:

    |-userExtra : 
        |-userId1 : 
           |- userId : "userId1",
           |- userName : "Name1",
           |-  (i and g)
           |...
    
        |-userId2 : 
           |- userId : "userId2",
           |- userName : "Name2",
           |-  (i and g)
           |...
    

    You can generate the geoHash with GeoHash geoHash = new GeoHash(location) and set the value to the child directly in the firebase.

    /*Java Code*/
    void setGeoFireMixData(UserExtra user, Location location){
       DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
       ref = ref.child("users").child(user.userId);
    
       //Setting userData if needed
       ref.child("userId").setValue(user.id);
       ref.child("Name").setValue(user.name);
       // .... other references
    
       //Setting GeoFire Data
       GeoHash geoHash = new GeoHash(location);
       ref.child("l").setValue(Arrays.asList(location.latitude, location.longitude));
       ref.child("g").setValue(geoHash.getGeoHashString());
    }
    

    2. Fetching mixed data with GeoFire

    About the question: Is this achievable via the key_entered method? Has anyone created a similar solution? Is this truly that bad of an idea even if the location info is consistently updated?

    I'd say, you can't retrieve both extra data and GeoFire using only the GeoQueryEventListener with onKeyEntered. You will need to use GeoQueryDataEventListener. For that, you can define the PoJo of your data considering the structure defined above.

    ...
    GeoQueryDataEventListener geoQueryDataEventListener = new GeoQueryDataEventListener() {
      @Override
      public void onDataEntered(DataSnapshot dataSnapshot, GeoLocation location) {
          UserExtra user = dataSnapshot.getValue(UserExtra.class);
          actionOnUser(user);
      }
      ...
    };
    ...
    geoQuery = geoFire.queryAtLocation(new GeoLocation(latitude,longitude), 0.5);
    geoQuery.addGeoQueryDataEventListener(geoQueryDataEventListener);
    ...
    

    The class UserExtra can be defined as below:

    public class UserExtra implements Serializable {
       String userId;
       String name;
        ...
       List l; //GeoLocation
       String g;
    }
    

    That will give you all the data in your node including the geoFire data.

提交回复
热议问题