如何在嵌套对象中获取Firestore GeoPoint值? [英] How to get Firestore GeoPoint value in nested Object?

查看:38
本文介绍了如何在嵌套对象中获取Firestore GeoPoint值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is my temporary code for generate geopoint datatype from 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

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结构

My Firestore Structure

  • 产品
  • -> {productIds}
  • ------>标题(字符串)
  • ------>位置(地图)
  • ----------> geoPoint(GeoPoint)
  • ------>地址(地图)
  • ----------> countryCode(字符串)

推荐答案

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

You post two different code and one of them provides you proper answer :

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

,此行代码显示null:

and this line of code shows null :

GeoPoint geoPoint = documentSnapshot.getGeoPoint(stringGeoPoint);

原因:

第一个代码后面的原因可以正常工作,因为在检索数据时,已经为documents字段创建了数据模型.您将您的方法设为 getLocation(),该方法将返回Location数组.然后,您通常可以通过传递hashmap的键来查看hashmap数组

The reason behind the first code works properly because when you retrieving data you have created data model for the documents field. you have you method as getLocation() which will return the Location array. Then you can normally look into hashmap array by passing the key of hashmap

Location是 Hashmap< String,String> 的数组.

Location is an Array of Hashmap<String, String>.

因此第二个代码对您不起作用,因为documentSnapshot下没有字段.

So the 2nd code won't work for you cause there is no field under the documentSnapshot.

这篇关于如何在嵌套对象中获取Firestore GeoPoint值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆