0

So im trying to develope a Geolocation android app first step is to get location ,latitude and longitude ..but when i use my methode "getlat" it returns 0.0 allways ,same with longitude.

Here is the database structure.

here is my methode to get latitude

public Double getlat(String email) {
    myRef=FirebaseDatabase.getInstance().getReference();
    DatabaseReference rootRef = myRef.child("users").child(email);

    rootRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists())
                latitude = dataSnapshot.child("lat").getValue(Double.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


    return latitude;
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Aymen Ch
  • 11
  • 3
  • Should that `gmail,com` be `gmail.com`? – algrid Jan 21 '18 at 14:45
  • @algrid Firebase Database keys cannot contains periods. It's a common practice to replace them with with commas when using email addresses in keys, since those conveniently can't contain commas. But it's a good point. Aymen: is `email` the email address with a comma? – Frank van Puffelen Jan 21 '18 at 15:16
  • really stupid question but are you using an emulator? – Pomagranite Jan 21 '18 at 16:13
  • @FrankvanPuffelen yeahh – Aymen Ch Jan 21 '18 at 16:53
  • 1
    As cutiko answered, data is loaded from Firebase (and most of the modern web) asynchronously. This means that by the time `onDataChange()` gets called, the `return latitude` statement has already executed. You can easily verify this by placing a few log statements and checking the order in which they show in logcat. The typical solutions are to either move the code that requires the latitude **into** `onDataChange()`, or to implement a callback interface that you call from `onDataChange()` (as shown [here](https://stackoverflow.com/q/33723139) and [here](https://stackoverflow.com/a/33204705) – Frank van Puffelen Jan 21 '18 at 18:17

1 Answers1

0

Two alternatives:

  1. If getLatitude is inside the Activity calling, make inside onDataChange the ui update
  2. Use an interface as callback

ValueEventListener isasynchronous which means it will be executed in other thread paralel to the method getLat. From top to bottom, getLat is called then the query to Firebase is movr to another thread and that code will be paralel to the rrst of the code inside the getLat method, wich means code execution inside getLat is still running, the next is the return statement. The return can happen before or after the Firrbase query has completed.

cutiko
  • 9,887
  • 3
  • 45
  • 59
  • but onDataChange won't accept it ..please more details ..thank you – Aymen Ch Jan 21 '18 at 16:58
  • @cutiko: good catch on the cause of the problem. You indeed can't return data that hasn't loaded yet. But moving the return inside `onDataChange(...)` wouldn't help, even if it was possible. By the time the data is loaded from Firebase, `getlat()` has already completed and nobody's listening for what `onDataChange(...)` might return. – Frank van Puffelen Jan 21 '18 at 18:14
  • Yeah, both are right. Have being switching to Javascript lately and got confused, let me take a second look – cutiko Jan 21 '18 at 18:28