Search code examples
androidfirebasefirebase-realtime-databasegeofiregoogle-geolocation

Does Geofire give new keys to each user?


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(); 
}

Solution

  • 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.