我正在尝试在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中查询集合的所有文档
发布评论