-1

I would like to use geofire for multiple location queries but don't know how to assign individual keys to each location. Does geofire automatically assign each user's location a different key?

Here is my Geofire code:

if (gps.canGetLocation()) {
 double latitude = gps.getLatitude(); 
 double longitude = gps.getLongitude(); 
 location.setText(latitude + "" + longitude); 
 //add points to database 
 myRef = database.getReference("Location"); 
 GeoFire geoFire = new GeoFire(myRef); 
 geoFire.setLocation("Person", new GeoLocation(latitude,longitude)); 
} else { 
    gps.showSettingsAlert(); 
}
Bread
  • 71
  • 1
  • 13
  • Can you show us the code you're using to update the key in firebase? – Rosário Pereira Fernandes Dec 24 '17 at 20:58
  • if (gps.canGetLocation()) { double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); location.setText(latitude + "" + longitude); //add points to database myRef = database.getReference("Location"); GeoFire geoFire = new GeoFire(myRef); geoFire.setLocation("Person", new GeoLocation(latitude,longitude)); } else { gps.showSettingsAlert(); } @RosárioPereiraFernandes – Bread Dec 28 '17 at 00:52
  • I just want to know if geofire automatically gives new keys to each user – Bread Dec 28 '17 at 00:54

1 Answers1

2

The answer to your question is No. Geofire doesn't give you a key for each user. You must use your own logic to create a key and then add it to a list (I'll call this list Locations):

String key = //use your own logic to get this
if (gps.canGetLocation()) {
 double latitude = gps.getLatitude(); 
 double longitude = gps.getLongitude(); 
 location.setText(latitude + "" + longitude); 
 //add points to the list on the database
 myRef = database.getReference("Locations").child(key);
 GeoFire geoFire = new GeoFire(myRef); 
 geoFire.setLocation("Person", new GeoLocation(latitude,longitude)); 
} else { 
    gps.showSettingsAlert(); 
}

If you're using Firebase Authentication, you can simply get the uid from the authenticated user and use it as your key.