如何在Fire Store中查询集合的所有文档

编程入门 行业动态 更新时间:2024-10-27 08:27:08
本文介绍了如何在Fire Store中查询集合的所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在Google地图上显示Cloud Firestore的信息.我可以使用以下代码在文档中应用标记,但是我需要显示多个文档.

I am trying to display information from Cloud Firestore on Google maps. I am able to apply a marker from a document using the below code, however I have multiple documents that I need to display the data from.

下面是当前代码,正如我所说的,它成功显示了一个文档中带有信息的标记,但是每个文档都包含一个需要在地图上显示的不同位置.

Below is the current code and as I said it successfully displays a marker with info from one document, however every document contains a different location that needs to be displayed on the map.

private void addMapMarkers(){ FirebaseFirestore rootRef = FirebaseFirestore.getInstance(); CollectionReference pointsRef = rootRef.collection("notes"); DocumentReference docRef = pointsRef.document("u20UmWSrlP0EiZH9CNP3"); docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { @Override public void onComplete(@NonNull Task<DocumentSnapshot> task) { if (task.isSuccessful()) { DocumentSnapshot document = task.getResult(); if (document.exists()) { String latitude = document.getString("Latitude"); String longitude = document.getString("Longitude"); String name = document.getString("Attraction Name"); float lat = Float.parseFloat(latitude); float lng = Float.parseFloat(longitude); LatLng latLng = new LatLng(lat, lng); mMaps.addMarker(new MarkerOptions().position(latLng).title(name)); } } } }); }

数据库结构如下:

推荐答案

要获取所有文档的纬度和经度属性的值,只需使用以下几行代码:

To get the values of your latitude and longitude properties of all documents, simply use the following lines of code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance(); CollectionReference notesRef = rootRef.collection("notes"); notesRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { if (task.isSuccessful()) { for (QueryDocumentSnapshot document : task.getResult()) { float lat = Float.parseFloat(document.getString("Latitude")); float lng = Float.parseFloat(document.getString("Longitude")); String name = document.getString("Attraction Name"); LatLng latLng = new LatLng(lat, lng); mMaps.addMarker(new MarkerOptions().position(latLng).title(name)); } } } });

因此解决此问题的关键是使用 task.getResult(),它可以帮助您遍历 QueryDocumentSnapshot 对象.

So the key for solving this problem is the use of task.getResult() that can help you iterate through the QueryDocumentSnapshot object.

更多推荐

如何在Fire Store中查询集合的所有文档

本文发布于:2023-11-22 20:19:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   如何在   Fire   Store

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!