Search code examples
androidfirebasefirebase-realtime-databasegeofire

Unable To Retrieve Location GeoFire Firebase Android


I'm trying to retrieve a GeoFire location saved on Firebase but it's returning null. Tried saving the location lat & lng with a global & local double variable but still the same.

Common.worker_tracking_table = "WorkerTracking"

Common.workerType = "Carpenters"

Already verified that these values are not null.

final DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference().child(Common.worker_tracking_table).child(Common.workerType);
        final GeoFire geoFire = new GeoFire(dbRef);

        geoFire.getLocation(Common.workerId, new LocationCallback() {
            @Override
            public void onLocationResult(String key, GeoLocation location) {
                if(location != null) {
                    Common.updatedLat = location.latitude;
                    Common.updatedLng = location.longitude;
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
}

Here's the database:


Solution

  • This is weird and I can't even explain how in the world it's possible but I was able to retrieve the location by removing the if statement leaving it like this:

    final DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference().child(Common.worker_tracking_table).child(Common.workerType);
        final GeoFire geoFire = new GeoFire(dbRef);
    
        geoFire.getLocation(Common.workerId, new LocationCallback() {
            @Override
            public void onLocationResult(String key, GeoLocation location) {
                Common.updatedLat = location.latitude;
                Common.updatedLng = location.longitude;
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        });
    }