Java 如何从firestore检索纬度和经度并在下面的函数中使用它?
所以我试图计算方向,但我不知道如何从firestore中检索纬度和经度,并将其用作下面的起点,下面是我尝试过的东西。它不工作,会崩溃,所以如果有人知道如何从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
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是异步的。因此,请检查我在以下帖子中的回答,以更好地理解:
45.4378090000001, 15.5524778