【问题标题】:Show alert dialog onMarkerClickListener在MarkerClickListener 上显示警报对话框
【发布时间】:2021-03-08 13:34:06
【问题描述】:

我有一个 for 循环,它为我的数据库中的每个条目绘制标记。但是,我想这样做,以便在单击时,每个标记在材料警报对话框上显示特定于该标记的信息(从数据库中检索的信息)。

现在,我正在尝试通过在创建标记时将我想要的信息放在标记中的标记中来做到这一点。然后,在创建 Alert Dialog 时,我从标签中获取信息并将其放在 Dialog 上。

但是,这没有奏效。当我单击一个标记时,无论它是什么标记,它总是直接转到单击侦听器中的 else 语句 - 它显示一条 Toast 消息,说“其他”。

没有错误消息,但它不起作用。

如何解决此问题,以便在单击每个标记时在警报对话框上显示每个标记的特定信息?

这是我的代码:

redColRef.get(source)
   .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
       @Override
       public void onComplete(@NonNull Task<QuerySnapshot> task) {
           if (task.isSuccessful()) {
               for (QueryDocumentSnapshot document : task.getResult()) {
                   java.util.Map<String, Object> mapOfPins = (HashMap<String, Object>) document.get("Location");


                   if (mapOfPins != null) {
                       Log.d(TAG, mapOfPins.get("latitude") + ", " + mapOfPins.get("longitude"));
                      pins = new LatLng(Double.valueOf(mapOfPins.get("latitude").toString()), Double.valueOf(mapOfPins.get("longitude").toString()));
                       direction = document.get("Direction").toString();
                       sender = document.get("Sender").toString();
                       java.util.Map<String, Object> detailsTag = new HashMap();
                       detailsTag.put("Location", pins);
                       detailsTag.put("Direction", direction);
                       detailsTag.put("Sender", sender);
                       switch (direction) {
                           case "North":
                           case "South":
                               map.addMarker(new MarkerOptions()
                                       .title("Pole Pin")
                                       .zIndex(0.6f)
                                       .icon(BitmapDescriptorFactory.fromResource(R.drawable.yellow))
                                       .position(pins));
// I realize that the "setTag()" is outside of the "addMarker()" so it doesn't work. But, I'm not sure how to fix that so that the tag is set.
                                       .setTag(detailsTag);

                               break;
                           case "East":
                           case "West":
                               map.addMarker(new MarkerOptions()
                                       .title("East-West Pin")
                                       .zIndex(0.7f)
                                       .icon(BitmapDescriptorFactory.fromResource(R.drawable.orange))
                                       .position(pins))
                                       .setTag(detailsTag);
                               break;
                           case "Other":
                               map.addMarker(new MarkerOptions()
                                       .title("Other Pin")
                                       .zIndex(0.8f)
                                       .icon(BitmapDescriptorFactory.fromResource(R.drawable.red))
                                       .position(pins))
                                       .setTag(detailsTag);
                               break;

                       }

                   }
               }
           } else {
               Log.d(TAG, task.getException().getMessage());
           }
       }
   });

map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

                               @Override
                               public boolean onMarkerClick(Marker marker) {
                                   if (marker.getTag() != null) {
                                       if (marker.getTitle().toString() == "Pole Pin") {

                                           java.util.Map markerDetails= (HashMap) marker.getTag();

                                       new MaterialAlertDialogBuilder(getActivity())
                                               .setTitle("Pole Pin")
                                               .setMessage("Location: " + markerDetails.get("Location").toString() + "\n Direction: " + markerDetails.get("Direction") + "\n Sender: " + markerDetails.get("Sender"))
                                               .setPositiveButton("View Details", new DialogInterface.OnClickListener() {
                                                   @Override
                                                   public void onClick(DialogInterface dialog, int which) {
                                                       Intent inte = new Intent(getActivity(), login.class);
                                                       startActivity(inte);
                                                   }
                                               })
                                               .show();
                                   } else {
                                           Toast.makeText(getActivity(), "Other", Toast.LENGTH_SHORT).show();
                                       }
                               }

                                   return true;
                               }
                           });

如果有人能帮助我,我将不胜感激!

提前谢谢你。

【问题讨论】:

  • 您的代码不起作用吗?有什么问题?
  • 那么这不意味着if 部分是null 吗?
  • @PrinceAli 是的,if 部分可能为空。但是,我对为什么以及如何解决这个问题感到困惑。如果标记的标题是“Pole Pin”,那么它不应该为空,对吧?我做错了什么?有什么想法吗?

标签: android google-maps material-ui google-maps-markers


【解决方案1】:

设置标签的正确方法是获取addMarkerMarker结果,然后调用.setTag()

Marker m = map.addMarker(
                 new MarkerOptions()
                       .title("Pole Pin")
                       .zIndex(0.6f)
                       .icon(BitmapDescriptorFactory.fromResource(R.drawable.yellow))
                       .position(pins));
m.setTag(detailsTag);

对每种情况都做同样的事情。

这是Marker 类的 API:https://developers.google.com/android/reference/com/google/android/gms/maps/model/Marker


我也注意到你的字符串比较是错误的,改用这个:

if (marker.getTitle().equals("Pole Pin")) {
    //...
}

简而言之,== 是引用相等(同一个实例),equals 是值相等。 (您也不需要toString。)供参考:https://stackoverflow.com/a/513839/2711811

【讨论】:

  • 感谢您的回复!但是,如果我没有单独定义每个标记,我仍然对如何在循环中为每个标记添加不同的标记感到困惑?
  • 当你添加一个标记时,它会返回一个新的标记对象。只需按上述方式更新您的代码(以接收结果并设置标签),每个添加的标记都将被分配在循环内创建的相应 detailsTag。
  • 非常感谢!这解决了它。
猜你喜欢
  • 2023-03-13
  • 2020-01-14
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多