英文:
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")
处。
我认为当你使用 getData()
时,它会将文档的字段返回为 Map。
那么,为什么 arrayOfPins
中的元素不是 Map 呢?
英文:
I sent a Map<String, LatLng>
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<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());
}
}
}
});
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("Location");
map.addMarker(new MarkerOptions()
.position(pCoord)
.title("Pin"));
}
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("Location")
.
Here is a picture of my Firestore Database:
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<String, Object>
对象。然而,要获取Location
属性下的数据,你应该在QueryDocumentSnapshot对象上显式调用.get("Location")
,就像以下代码中所示:
pinColRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Map<String, Object> map = (HashMap<String, Object>) document.get("Location");
if (map != null) {
Log.d("TAG", map.get("latitude") + ", " + map.get("longitude"));
// 在地图上添加标记
}
}
} 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<String, Object>
object. However, to get the data under Location
property, you should explicitly call .get("Location")
on the QueryDocumentSnapshot object, like in the following lines of code:
pinColRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Map<String, Object> map = (HashMap<String, Object>) document.get("Location");
if (map != null) {
Log.d("TAG", map.get("latitude") + ", " + map.get("longitude"));
//Add the markers on the map
}
}
} else {
Log.d(TAG, task.getException().getMessage());
}
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论