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

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

Error retrieving data from Cloud Firestore database

问题

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

这是我所做的:

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

如何检索数据:
MapsActivity:

Source source = Source.DEFAULT;

pinColRef.get(source)
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                       ArrayList arrayOfPins = new ArrayList<java.util.Map>();
                       arrayOfPins.add(document.getData());
                       Log.i(TAG, document.getData().toString());
                    }
                }
            }
        });

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

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

这是我如何做到的:

MapsActivity:

public ArrayList getPinData() {
    return arrayOfPins;
}

Fragment:

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

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

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

第一个错误出现在 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:

Source source = Source.DEFAULT;

pinColRef.get(source)
        .addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
            @Override
            public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                       ArrayList arrayOfPins = new ArrayList&lt;java.util.Map&gt;();
                       arrayOfPins.add(document.getData());
                       Log.i(TAG, document.getData().toString());
                    }
                }
            }
        });

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:

public ArrayList getPinData() {
    return arrayOfPins;
}

Fragment:

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

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

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

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;),就像以下代码中所示:

pinColRef.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
    @Override
    public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                Map&lt;String, Object&gt; map = (HashMap&lt;String, Object&gt;) document.get(&quot;Location&quot;);
                if (map != null) {
                    Log.d(&quot;TAG&quot;, map.get(&quot;latitude&quot;) + &quot;, &quot; + map.get(&quot;longitude&quot;));

                    // 在地图上添加标记
                }
            }
        } else {
            Log.d(TAG, task.getException().getMessage());
        }
    }
});
英文:

> 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:

pinColRef.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
    @Override
    public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                Map&lt;String, Object&gt; map = (HashMap&lt;String, Object&gt;) document.get(&quot;Location&quot;);
                if (map != null) {
                    Log.d(&quot;TAG&quot;, map.get(&quot;latitude&quot;) + &quot;, &quot; + map.get(&quot;longitude&quot;));

                    //Add the markers on the map
                }
            }
        } else {
            Log.d(TAG, task.getException().getMessage());
        }
    }
});

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:

确定