【问题标题】:Remove marker and at the same time the document in firestore删除标记并同时删除firestore中的文档
【发布时间】:2020-08-15 06:10:24
【问题描述】:

当我创建一个标记时,我以某种方式希望链接到它的 FirebaseFirestore 文档的相关信息跟随它,以便稍后当我想要删除该标记时,FirebaseFirestore 标记会随之删除。

在我的情况下,当按下标记并且用户长按是 infoWindow 时,它应该删除标记以及文档。 onInfoWindowLongClickListener 的逻辑如下:

gMap.setOnInfoWindowLongClickListener(new GoogleMap.OnInfoWindowLongClickListener() { 
    @Override
    public void onInfoWindowLongClick(Marker marker) {
        FirebaseFirestore db = FirebaseFirestore.getInstance(); // I guess this is needed.
        marker.remove(); // removes marker
    }
});

标记已删除,但我仍然可以在 Firestore 中看到该文档。

我应该使用marker.getTag()吗?

当我创建标记时,它会变成这样:

btnRetrieveMarkers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FirebaseFirestore db = FirebaseFirestore.getInstance();
            db.collection("my_collection")
                    .get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if(task.isSuccessful()){
                                for(QueryDocumentSnapshot documentSnapshot : task.getResult()){
                                    HashMap hashMap = (HashMap) documentSnapshot.getData();
                                    String status = (String) hashMap.get("Status");
                                    GeoPoint geo = (GeoPoint) hashMap.get("Geopoint");

                                    LatLng latLng = new LatLng(geo.getLatitude(), geo.getLongitude());

                                    Marker mark =  gMap.addMarker(new MarkerOptions().position(latLng).title(status)));
                                    mark.setDraggable(true);
                                    // mark.setTag() < -- how to save the info about the document here?
                                    }
                            }else {
                                // TODO: handle error here
                            }
                            if (task.getResult().isEmpty()) {
                                Toast.makeText(MapsActivity.this, R.string.no_markers_registered_yet, Toast.LENGTH_LONG).show();
                            }
                        }
                    });

更新:问题已被编辑/更新以阐明目的,因为我对此不够清楚。

【问题讨论】:

  • Firestore 有一个用于删除文档的 API。看起来你没有使用它。 firebase.google.com/docs/firestore/manage-data/delete-data
  • @DougStevenson 我不知道如何指定它应该删除哪个文档。如果我只输入 db.collection("collectionA").document().delete();没有任何反应 - 文档仍在 Firestore 中。
  • 好吧,我们也不知道您需要删除哪个文档。这里没有足够的信息让任何人将标记与它来自的文档相关联。
  • @DougStevenson 这是我的问题 - 我不知道如何传输/获取信息..
  • @DougStevenson 我已经更新了我的问题,所以现在应该更清楚了。请重新考虑是否投反对票。

标签: java android firebase google-maps google-cloud-firestore


【解决方案1】:

如果您想仅使用标记删除标记的 firestore 文档,请在创建标记时使用其 firestore 文档 ID 标记该标记。

Marker marker = mMap.addMarker(new MarkerOptions()
  ...
);

marker.setTag("[document id]");

当你想删除它时:

FirebaseFirestore db = FirebaseFirestore.getInstance();

String markerId = (String) marker.getTag();

db.collection("[name of the collection]")
.document(markerId)
.delete();

您可能还希望实现OnMarkerClickListener 以在单击时获取选定的标记。

【讨论】:

  • 这是正确的 - 稍作调整。我将编辑您的答案,使其正确。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-05-08
  • 2020-09-01
  • 2019-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
相关资源
最近更新 更多