Save geoFire location under random firebase id swift 4
If you want to store the geolocation under the same key as
let ref = userPostRef.childByAutoId()
You can simply get the key
property from ref. So:
geoFire?.setLocation(CLLocation(latitude: geoLatitude, longitude: geoLongitude), forKey: ref.key)
Integrating Geofire with Firebase Data
I figured it out. You create a unique Geofire node that mirrors the one you're trying to monitor.
Storing Extra Data Under GeoFire Nodes
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 :)
Related Topics
Adding Button Action in Custom Notification
How to Get Higher Precision of "Cpu%" Than That from Top Command
Force an Android Activity to Always Use Landscape Mode
Apply a Theme to an Activity in Android
Android Studio Error "Installed Build Tools Revision 31.0.0 Is Corrupted"
Calling Startactivity() from Outside of an Activity
How to Pass Drawable Between Activities
Listview Scroll to the End of the List After Updating the List
How to Return Value from Function Onresponse of Volley
No Internet on Android Emulator - Why and How to Fix
How to Get String from Different Locales in Android
"No Such File or Directory" Trying to Execute Linux Binary on Android Device
Set Inputtype for an Edittext Programmatically
Transitive Dependencies for Local Aar Library
Installation App Blocked by Play Protect