【问题标题】:How to convert GeoPoint in Firestore to LatLng如何将 Firestore 中的 GeoPoint 转换为 LatLng
【发布时间】:2019-05-16 21:11:12
【问题描述】:

您好,我正在使用 Android Studio,但在转换从 Firestore 获得的数据时遇到了麻烦。我将数据保存在我的 Firestore 类型 GeoPoint 中,我想查询它并转换为对象类型 LatLng

这是我的代码:

    final FirebaseFirestore db = FirebaseFirestore.getInstance();
    final CollectionReference stores = db.collection("stores");
    stores.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Log.i(TAG,document.getData().get("position").toString());
                        }
                    } else {
                        Log.w(TAG, "Error getting documents.", task.getException());
                    }
                }
            });

这是我得到的

    12-16 10:59:21.557 28239-28239/com.company.yummy.yummy I/firebaseCatch: GeoPoint { latitude=29.339555, longitude=169.715858 }
    12-16 10:59:21.557 28239-28239/com.company.yummy.yummy I/firebaseCatch: GeoPoint { latitude=68.085393, longitude=-42.081575 }
    12-16 10:59:21.557 28239-28239/com.company.yummy.yummy I/firebaseCatch: GeoPoint { latitude=16.503923, longitude=90.196118 }
    12-16 10:59:21.557 28239-28239/com.company.yummy.yummy I/firebaseCatch: GeoPoint { latitude=-69.424524, longitude=-71.356333 }
    12-16 10:59:21.557 28239-28239/com.company.yummy.yummy I/firebaseCatch: GeoPoint { latitude=69.502257, longitude=71.100474 }

我只需要获取纬度、经度值以设置到 LatLng 对象中。

【问题讨论】:

    标签: java android firebase google-cloud-firestore geopoints


    【解决方案1】:

    要解决这个问题,您可以像这样简单地使用getGeoPoint() 方法:

    GeoPoint geoPoint = document.getGeoPoint("position");
    

    然后使用:

    double lat = geoPoint.getLatitude();
    double lng = geoPoint.getLongitude();
    LatLng latLng = new LatLng(lat, lng);
    

    【讨论】:

    • 如果在嵌套对象中呢?产品 products = documentSnapshot.toObject(Products.class); System.out.println(products.getLocation().get("geoPoint").toString());
    • @LatiefAnwar 在这种情况下,您应该简单地使用:System.out.println(products.getLocation().toString());,它将返回一个 Geopoint 对象。如果您很难解决这个问题,请使用自己的 MCVE 发布另一个新问题,以便我和其他 Firebase 开发人员可以帮助您。
    • 我们不能通过 LatLng 来简化它吗 latLng = new LatLng(document.getGeoPoint("position").getLatitude(), document.getGeoPoint("position").getLongitude());
    • @IzhanAli 如果你愿意,你可以做到。
    【解决方案2】:

    这对 Flutter cloud_firestore 很有效

    void getData() {
    databaseReference
        .collection("stores")
        .getDocuments()
        .then((QuerySnapshot snapshot) {
      snapshot.documents.forEach((f) {
        print('${f.data}}');
        GeoPoint pos = f.data['position'];
        LatLng latLng = new LatLng(pos.latitude, pos.longitude);
    
      });
    });
    }
    

    参考 - https://fireship.io/lessons/flutter-realtime-geolocation-firebase/

    【讨论】:

      【解决方案3】:

      您需要从收到的 Geopoint 对象中提取纬度和经度,

      然后创建一个新的 LatLng 对象,像这样

      double lat = document.getData().get("position").getLatitude();
      double lng = document.getData().get("position").getLongitude ();
      LatLng latLng = new LatLng(lat, lng);
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 2010-09-25
      相关资源
      最近更新 更多