从Cloud Firestore数据库检索数据时出错。

huangapple go评论112阅读模式
英文:

Error retrieving data from Cloud Firestore database

问题

我将一个 Map<String, LatLng> 发送到了我的 Cloud Firestore 数据库。现在,我想要将数据检索为一个 Map。然后,我想将该 Map 发送到我的 Fragment,以便 Fragment 可以在 Google 地图上标出 LatLng。

这是我所做的:

我成功地从数据库中检索到了数据,但错误出现在 Fragment 中,我希望从 Map 中获取 LatLng 的地方。

如何检索数据:
MapsActivity:

  1. Source source = Source.DEFAULT;
  2. pinColRef.get(source)
  3. .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
  4. @Override
  5. public void onComplete(@NonNull Task<QuerySnapshot> task) {
  6. if (task.isSuccessful()) {
  7. for (QueryDocumentSnapshot document : task.getResult()) {
  8. ArrayList arrayOfPins = new ArrayList<java.util.Map>();
  9. arrayOfPins.add(document.getData());
  10. Log.i(TAG, document.getData().toString());
  11. }
  12. }
  13. }
  14. });

正如您所见,我检索数据,然后使用 getData() 来获取我发送的 Map。Log.i 语句成功显示了来自数据库的数据,其格式为 Map。

然后,我将该数据发送到了 Fragment。

这是我如何做到的:

MapsActivity:

  1. public ArrayList getPinData() {
  2. return arrayOfPins;
  3. }

Fragment:

  1. MapsActivity activity = (MapsActivity) getActivity();
  2. ArrayList arrayOfPins = activity.getPinData();

我如何尝试在数据库中标出 LatLng(在 Fragment 的 MapReadyCallback 中):

  1. for (java.util.Map ploc : arrayOfPins) {
  2. LatLng pCoord = ploc.get("Location");
  3. map.addMarker(new MarkerOptions()
  4. .position(pCoord)
  5. .title("Pin"));
  6. }

第一个错误出现在 java.util.Map ploc 处:

错误:不兼容的类型:无法将 Object 转换为 Map

然后,另一个错误出现在 LatLng pCoord = ploc.get("Location") 处。

这是我 Firestore 数据库的截图:
从Cloud Firestore数据库检索数据时出错。

我认为当你使用 getData() 时,它会将文档的字段返回为 Map。

那么,为什么 arrayOfPins 中的元素不是 Map 呢?

英文:

I sent a Map&lt;String, LatLng&gt; to my Cloud Firestore database. Now, I want to retrieve the data as a Map. Then, I want to send that Map to my fragment so that the fragment can plot the LatLng on a google map.

Here's what I've done:

I successfully retrieved the data from the database, but the error comes in the fragment, where I want to get the LatLng from the Map.

How I retrieve the data:
MapsActivity:

  1. Source source = Source.DEFAULT;
  2. pinColRef.get(source)
  3. .addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
  4. @Override
  5. public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
  6. if (task.isSuccessful()) {
  7. for (QueryDocumentSnapshot document : task.getResult()) {
  8. ArrayList arrayOfPins = new ArrayList&lt;java.util.Map&gt;();
  9. arrayOfPins.add(document.getData());
  10. Log.i(TAG, document.getData().toString());
  11. }
  12. }
  13. }
  14. });

As you see, I retrieve the data and then use getData() to get the Map I sent. The Log.i statement successfully shows the data from the database as a Map.

So, I send that data to the fragment.

Here's how I did that:

MapsActivity:

  1. public ArrayList getPinData() {
  2. return arrayOfPins;
  3. }

Fragment:

  1. MapsActivity activity = (MapsActivity) getActivity();
  2. ArrayList arrayOfPins = activity.getPinData();

How I try to plot the LatLng from the database (in the MapReadyCallback in the Fragment):

  1. for (java.util.Map ploc : arrayOfPins) {
  2. LatLng pCoord = ploc.get(&quot;Location&quot;);
  3. map.addMarker(new MarkerOptions()
  4. .position(pCoord)
  5. .title(&quot;Pin&quot;));
  6. }

The first error comes on the java.util.Map ploc:

> error: incompatible types: Object cannot be converted to Map

Then, the other error is on LatLng pCoord = ploc.get(&quot;Location&quot;).

Here is a picture of my Firestore Database:
从Cloud Firestore数据库检索数据时出错。

I thought that when you use getData() it returns the fields of the document as a Map.

So, why are the elements in arrayOfPins not Maps?

答案1

得分: 1

> 我认为当你使用getData()时,它会将文档的字段作为一个Map返回。

确实,QueryDocumentSnapshot的getData()方法会返回一个Map&lt;String, Object&gt;对象。然而,要获取Location属性下的数据,你应该在QueryDocumentSnapshot对象上显式调用.get(&quot;Location&quot;),就像以下代码中所示:

  1. pinColRef.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
  2. @Override
  3. public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
  4. if (task.isSuccessful()) {
  5. for (QueryDocumentSnapshot document : task.getResult()) {
  6. Map&lt;String, Object&gt; map = (HashMap&lt;String, Object&gt;) document.get(&quot;Location&quot;);
  7. if (map != null) {
  8. Log.d(&quot;TAG&quot;, map.get(&quot;latitude&quot;) + &quot;, &quot; + map.get(&quot;longitude&quot;));
  9. // 在地图上添加标记
  10. }
  11. }
  12. } else {
  13. Log.d(TAG, task.getException().getMessage());
  14. }
  15. }
  16. });
英文:

> I thought that when you use getData() it returns the fields of the document as a Map.

Indeed, QueryDocumentSnapshot's getData() method returns a Map&lt;String, Object&gt; object. However, to get the data under Location property, you should explicitly call .get(&quot;Location&quot;) on the QueryDocumentSnapshot object, like in the following lines of code:

  1. pinColRef.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
  2. @Override
  3. public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
  4. if (task.isSuccessful()) {
  5. for (QueryDocumentSnapshot document : task.getResult()) {
  6. Map&lt;String, Object&gt; map = (HashMap&lt;String, Object&gt;) document.get(&quot;Location&quot;);
  7. if (map != null) {
  8. Log.d(&quot;TAG&quot;, map.get(&quot;latitude&quot;) + &quot;, &quot; + map.get(&quot;longitude&quot;));
  9. //Add the markers on the map
  10. }
  11. }
  12. } else {
  13. Log.d(TAG, task.getException().getMessage());
  14. }
  15. }
  16. });

huangapple
  • 本文由 发表于 2020年8月30日 07:26:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63652693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定