首页 > 解决方案 > 如何使用 GeoFirestore (Java/Android) 在谷歌地图上显示多个标记?

问题描述

我正在尝试使用 在我的地图片段上添加多个标记GeoFirestore,但我不明白该怎么做。我尝试按照他们网站的指导进行操作,但仍然无法获得预期的结果。

如果集合中的多个文档在所需范围内,我希望在地图上显示它们;但是,我不知道应该在哪里实例化标记。

Firestore 中的数据库结构:

在此处输入图像描述

GeoFirebase 查询代码:

if (distantCategoryValue != null) {
        switch (distantCategoryValue) {
            case "6 Km":

                CollectionReference geoFirestoreRef = FirebaseFirestore.getInstance().collection("Events");
                GeoFirestore geoFirestore = new GeoFirestore(geoFirestoreRef);
                GeoQuery geoQuery = geoFirestore.queryAtLocation(new GeoPoint(currentLocation.getLatitude(), currentLocation.getLongitude()), 6);
                geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
                    @Override
                    public void onDocumentEntered(DocumentSnapshot documentSnapshot, final GeoPoint geoPoint) {

                    }

                    @Override
                    public void onDocumentExited(DocumentSnapshot documentSnapshot) {

                    }

                    @Override
                    public void onDocumentMoved(DocumentSnapshot documentSnapshot, GeoPoint geoPoint) {

                    }

                    @Override
                    public void onDocumentChanged(DocumentSnapshot documentSnapshot, GeoPoint geoPoint) {

                    }

                    @Override
                    public void onGeoQueryReady() {
                    }

                    @Override
                    public void onGeoQueryError(Exception e) {

                    }

                });

                break;
        }

标签: javaandroidfirebasegoogle-cloud-firestoregeofirestore

解决方案


前四个覆盖方法中的每一个都有一个DocumentSnapshot包含一些数据的对象作为第一个参数。根据在特定区域内发生的操作调用每个方法。现在,要获取该数据,您可以使用 DocumentSnapshot 的getData()方法,该方法的返回类型为 a Map<String, Object>。只需遍历地图并获取l属性,该属性是一个包含纬度和经度的数组。第二种方法是使用 DocumentSnapshot 的toObject(Class valueType)并将每个DocumentSnapshot对象转换为一个Event对象。获得数据后,使用以下代码行将其添加到地图中:

Event event = documentSnapshot.getValue(Event.class);
LatLng latLng = new LatLng(event.getLatitude(), event.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng));

推荐阅读