static double lat2;
static double lon2;
private void InitMapElements() {
DatabaseReference zonesRef = FirebaseDatabase.getInstance().getReference("markers");
DatabaseReference zone1Ref = zonesRef.child("m-1"); //database path
DatabaseReference zone1NameRef = zone1Ref.child("latit");
DatabaseReference zone2NameRef = zone1Ref.child("longit");
zone1NameRef.addValueEventListener(new ValueEventListener() { //reading the first coordinate
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Double lat1 = dataSnapshot.getValue(Double.class);
lat2 = lat1;
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
zone2NameRef.addValueEventListener(new ValueEventListener() { //reading the second coordinate
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Double lon1 = dataSnapshot.getValue(Double.class);
lon2 = lon1;
}
@Override
public void onCancelled(DatabaseError error) {
}
});
}
It is not possible to get two variables online and then save it to a static variable from the Firebase realtime database, the variables get the value only once when the method is called. With one variable, everything works fine. The variable changes if you change the value on the base, but with two does not work.