Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing Extra Data Under GeoFire Nodes

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

geofire
  -Ke1uhoT3gpHR_VsehIv
  -Kdrel2Z_xWI280XNfGg
    -g: "dr5regw90s"
    -l
      -0: 40.7127837
      -1: -74.00594130000002

It is avised to store a location's information and geofire data in separate nodes, however I see an advantage to storing some extra data under these geofire nodes, such as the name of a business. This way I'd only have to make only one call to my Firebase to fetch nearby locations as well as their names.

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?

Any input is appreciated!

like image 236
Clay Banks Avatar asked Dec 10 '22 12:12

Clay Banks


1 Answers

Firstly the structure you are using for the app is wrong.

let us take an example of users app

When you use Geofire, you have two lists of data:

a list of user details for each user
a list of latitude and longitude coordinates for each user

You are trying to store both in same structure like this

"users" : {
    <userId> : {
        "userData" : "userData",
         <geofireData> 
        ...
    }
}

Trying to store userdata and geofiredata in one node is a bad idea, since you're mixing mostly static data (the properties of your user) with highly volatile data (the geo-location information).Separating the two out leads to better performance, which is why Geofire enforces it.

This is why when you try to add geolocation data to a user node it overwrites previous user details for that node.

Your database structure should be like this.

"Users" : {
    <UserId> : {
        "UserData" : "UserData",
        ...
    }
}
"Users_location" : {
    <UserId> : {
        <geofireData> ...
    }
}

Hence for the same user Id you create 2 structures one for user details and another for geolocation details of that user.

How to push the user and set the geofire data.

String userId = ref.child("users").push().getKey();

ref.child("users").child(userId).setValue(user);

geoFire = new GeoFire(ref.child("user_location"));
geoFire.setLocation(userId, new GeoLocation(lattitude, longitude));

the userId you use in geolocation is same as the one you got during push().

Hence for each userId you have userdetails in one structure and location details in anotehr structure.

To get the data, first you need to do GeoQuery at users_location node and then get the data on the onKeyEntered method. The parameter key is userId from my example.

geoFire=newGeoFire(FirebaseDatabase.getInstance().getReference().child("users_location");
geoQuery = geoFire.queryAtLocation(geoLocation), radius);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
    @Override
    public void onKeyEntered(String key, GeoLocation location) {
         //retrieve data
//use this key which is userId to fetch user details from user details structure
    }
};    

Happy coding :)

like image 75
Charan Reddy Avatar answered Jan 16 '23 14:01

Charan Reddy