Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从firestore检索纬度和经度并在下面的函数中使用它?_Java_Android_Google Maps_Google Cloud Firestore - Fatal编程技术网

Java 如何从firestore检索纬度和经度并在下面的函数中使用它?

Java 如何从firestore检索纬度和经度并在下面的函数中使用它?,java,android,google-maps,google-cloud-firestore,Java,Android,Google Maps,Google Cloud Firestore,所以我试图计算方向,但我不知道如何从firestore中检索纬度和经度,并将其用作下面的起点,下面是我尝试过的东西。它不工作,会崩溃,所以如果有人知道如何从firestore保存纬度和经度,然后将其放入下面的函数中,请帮助 private void calculateDirections() { Log.d(TAG, "calculateDirections: calculating directions."); direction_ref_end.get

所以我试图计算方向,但我不知道如何从firestore中检索纬度和经度,并将其用作下面的起点,下面是我尝试过的东西。它不工作,会崩溃,所以如果有人知道如何从firestore保存纬度和经度,然后将其放入下面的函数中,请帮助

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

    );
private void calculateDirections(){
Log.d(标签,“计算方向:计算方向”);
方向\u ref\u end.get().addOnSuccessListener(新OnSuccessListener()){
@凌驾
成功时公共无效(文档快照文档快照){
//latitude_end=(双精度)documentSnapshot.getData().get(“纬度”);
//经度=documentSnapshot.getData().get(“经度”);
}
});
com.google.maps.model.LatLng destination=新建com.google.maps.model.LatLng(
//纬度结束,经度结束
);

要从“结束”文档中获取纬度和经度,请使用以下代码行:

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());
        }
    }
});
除此之外,请注意,您不能简单地在回调之外使用这些坐标的值。这是因为Firebase API是异步的。因此,请检查我在以下帖子中的回答,以更好地理解:


我认为这将解决你的问题:如果你不确定你在做什么,你可能首先要考虑阅读文档。请只使用<代码> Android Studio标签来询问Android StudioIDE本身。对于Android编程的一般问题,请使用<代码> Android < /Calp>标签。这是真的。很难帮助“它不工作,它崩溃”。如果出现崩溃,请在应用程序的logcat输出中找到错误消息和堆栈跟踪,并将它们添加到问题中。如果应用程序崩溃,则存在堆栈跟踪。请在logcat上查找该消息,并将其添加到您的问题中。请使用@AlexMamoHey Kolarka!您尝试过我的上述解决方案吗?是否有效?如果您认为我的答案是h呵呵,请您通过点击复选来接受它。✔️) 在投票箭头下方的左侧。应该将颜色更改为绿色。我非常感谢。谢谢!
45.4378090000001, 15.5524778