0

Here is my firestore

So I'm trying to calculate directions and i don't know how to retrieve latitude and longitude from firestore and the use it for starting point below, here is something that i tried. It doesn't work, it crashes, so if someone know how to save latitude and longitude from firestore and then put it in function below please help.

private void calculateDirections() {
    Log.d(TAG, "calculateDirections: calculating directions.");


    direction_ref_end.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
          //  latitude_end = (Double) documentSnapshot.getData().get("latitude");
          //  longitude_end = (Double) documentSnapshot.getData().get("longitude");



        }
    });
    com.google.maps.model.LatLng destination = new com.google.maps.model.LatLng(
                //    latitude_end, longitude_end

    );
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Kolarka
  • 11
  • 1
  • I think this will solve your problem: [Getting data from firestore](https://stackoverflow.com/a/47924071/12715840) – Avinash Tingre Apr 08 '21 at 10:08
  • If you're not sure what you're doing at all, you might want to consider reading the documentation first. https://firebase.google.com/docs/firestore/query-data/get-data – Henry Twist Apr 08 '21 at 11:27
  • Please only use the `android-studio` tag for questions about the Android Studio IDE itself. For questions about Android programming in general, use the `android` tag. – Frank van Puffelen Apr 08 '21 at 14:06
  • It is really hard to help with "It doesn't work, it crashes". If there is a crash, find the error message and stack trace in your app's logcat output and add them to the question please. – Frank van Puffelen Apr 08 '21 at 14:07
  • If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question. Please respond with @AlexMamo – Alex Mamo Apr 08 '21 at 14:54

1 Answers1

1

To get the latitude and longitude from your "End" document, please use the following lines of code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference directionRef = rootRef.collection("Direction");
DocumentReference endRef = directionRef.document("End");
endRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                double lat = document.getDouble("latitude");
                double lng = document.getDouble("longitude");
                Log.d("TAG", lat + ", " + lng); //Do what you need to do with these coordinates
            } else {
                Log.d("TAG", "No such document");
            }
        } else {
            Log.d("TAG", "get failed with ", task.getException());
        }
    }
});

The result in the logcat will be:

45.4378090000001, 15.5524778

Besides that, be aware that you cannot simply use the values of those coordinates outside the callback. This is because Firebase API is asynchronous. So please check my answer from the following post for a better understanding:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193