【问题标题】:How to get Firestore GeoPoint value in nested Object?如何在嵌套对象中获取 Firestore GeoPoint 值?
【发布时间】:2019-09-23 10:54:58
【问题描述】:

这是我从 Firestore DB 生成地理点数据类型的临时代码

firestoreDb.collection("products").document(productId).get()
  .addOnSuccessListener(documentSnapshot -> {
     Products products = documentSnapshot.toObject(Products.class); 
     countryCode = Objects.requireNonNull(products.getAddress().get("countryCode")).toString();

     String stringGeoPoint = products.getLocation().get("geoPoint").toString();
     System.out.println(stringGeoPoint);
     //Output: GeoPoint { latitude=29.339555, longitude=169.715858 }

     GeoPoint geoPoint = documentSnapshot.getGeoPoint(stringGeoPoint);// ?
     //Output: null
  }

Products.class

public class Products{ 
   public Map<String, Object> address;
   public Map<String, Object> location;

   public Map<String, Object> getAddress(){return address;}
   public Map<String, Object> getLocation(){return location;}
}

我的 Firestore 结构

  • 产品
  • -->{productIds}
  • ------>标题(字符串)
  • ------>位置(地图)
  • ---------->地理点(GeoPoint)
  • ------>地址(地图)
  • ---------->国家代码(字符串)

【问题讨论】:

  • 不要描述你的数据库的样子,而是添加它的截图。
  • 已编辑,添加截图。
  • Location 是数据数组。
  • @LatiefAnwar 所以你想获得存在于location 中的geoPoint 属性的值,对吧?还请添加您的Products 课程的内容并回复@AlexMamo

标签: android firebase google-cloud-firestore


【解决方案1】:

您发布了两个不同的代码,其中一个为您提供了正确的答案:

String stringGeoPoint = products.getLocation().get("geoPoint").toString();

而这行代码显示为空:

GeoPoint geoPoint = documentSnapshot.getGeoPoint(stringGeoPoint);

原因:

第一个代码背后的原因可以正常工作,因为当您检索数据时,您已经为文档字段创建了数据模型。你有你作为getLocation() 的方法,它将返回位置数组。然后你通常可以通过传递 hashmap 的键来查看 hashmap 数组

位置是Hashmap&lt;String, String&gt; 的数组。

所以第二个代码对你不起作用,因为 documentSnapshot 下没有字段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 2020-02-08
    相关资源
    最近更新 更多