0

我正在 Flutter firebase 中实施一个项目。在应用程序中,它会将用户在移动到 Firestore 数据库时所经过的地点的位置发送到该应用程序。我也想再次从数据库中获取这些位置,但我无法获取地理点代码。我遇到了一个名为“'Geopoints'的实例”的输入。我想要的是在“'Geopoints'实例”的输入部分中获取经度和纬度作为坐标。例如 (52.2165157N, 6.94378199E)

我们从数据库中检索到的代码:

getData() async {
FirebaseFirestore.instance
    .collection('Test')
    .where(('Liste'), arrayContains: "kosum1")
    .get()
    .then((value) {
  value.docs.forEach((result) {
     print(result.data().values.toString());
  });
});}

我们在数据库中记录的这段代码:

dataBaseSave() async {
final User user = auth.currentUser;
await _geolocator
    .getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
    .then((Position position) {
  setState(() {
    _currentPosition = position;
    GeoFirePoint point = geo.point(
        latitude: position.latitude, longitude: position.longitude);
    FirebaseFirestore.instance.collection('Test').add({
      'Liste': FieldValue.arrayUnion([nb1, kosuadi, user.uid, point.data])
    });
    nb1++;
  });
});}

当我以这种方式运行它时得到的输出: 在此处输入图像描述

这是我的数据库: 在此处输入图像描述

4

3 回答 3

1

正如我在您的屏幕截图中看到的,“测试”集合中的每个文档都包含一个数组。在位置 0 有一个数字,在位置 1 和 2 有一个字符串,在位置 3 有一个对象。该对象包含一个 geohash 和一个地理点。代码中最重要的部分是:

result.data()

它返回一个地图。因此,当使用以下代码行时:

print(result.data().values.toString());

您只打印地图的字符串表示。为了能够获取 geopoint 属性的值,您必须遍历 Map 并从第三个位置获取元素(对象),这在术语上也是一个 Map。再次迭代,将第二个元素作为 GeoPoint 对象。

于 2021-06-12T11:10:26.863 回答
0

您必须深入挖掘数据才能获取信息。result将是一个包含字符串(索引 0-2)和一个对象的数组。该对象看起来像是您的 GeoPoint 的一个实例。当您访问该地理点时,您应该能够做到myGeopoint.latitudemyGeopoint.longitude获得这两个点。这并不准确,但它看起来应该result.data().values[3].geopoint.longitude与经度相同。

于 2021-06-12T00:12:16.300 回答
0

我在将查询保存到数据库时更改了查询,这就是我的做法。那么在这种状态下我应该如何从数据库中获取查询呢?

dataBaseSave() async {
final User user = auth.currentUser;
await _geolocator
    .getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
    .then((Position position) {
  setState(() {
    _currentPosition = position;
    GeoFirePoint point = geo.point(
        latitude: position.latitude, longitude: position.longitude);
    FirebaseFirestore.instance.collection('Test').add({
      'Liste': FieldValue.arrayUnion(
          [nb1, kosuadi, user.uid, position.latitude, position.longitude])//I changed this place
    });
    nb1++;
  });
});}

和新的数据库数据: 在此处输入图像描述

于 2021-06-12T16:30:47.270 回答