从列表中隐藏一个标记不起作用 [英] Hiding just one marker from a list isn't working

查看:164
本文介绍了从列表中隐藏一个标记不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在地图上有一堆标记,他们从firebase实时数据库获取他们的数据。我在地图上有一个开关按钮,可以显示或隐藏所有标记,并且工作正常。但是现在我想要特定的标记来隐藏一些动作,但它不起作用。

真正发生的是当我触发某个标记的某个动作时,它会正常更新数据库,但它并没有实时隐藏地图。 !
我不得不关闭地图并再次打开以查看更改..!

什么问题..!?






守则

 标记studentMarker = null; 
ArrayList< Marker> studentsMarkers = new ArrayList<>();
ArrayList< String> stdId = new ArrayList<>();


@Override
public void onChildChanged(DataSnapshot snapshot,String s){
LatLng latLng = new LatLng(snapshot.getValue(TestUserStudent.class).getLat( ),
snapshot.getValue(TestUserStudent.class).getLang());
boolean isShow = snapshot.getValue(TestUserStudent.class).isShow();

if(isShow){
studentMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title(snapshot.getValue(TestUserStudent.class ).getName()+)
.snippet(snapshot.getValue(TestUserStudent.class).getSection()+)
.icon(BitmapDescriptorFactory.fromBitmap(
getMarkerBitmapFromView(snapshot .getValue(TestUserStudent.class).getImg(),
R.drawable.map_marker_red))));
studentsMarkers.add(studentMarker);
stdId.add(snapshot.getKey());
}
}


//当我点击列表中某个标记的infowindow中的按钮时调用此方法,并且它正确更新数据库中的标记!
void onWindowInfoButtonClicked(Marker marker){

for(int i = 0; i< studentsMarkers.size(); i ++){
if(marker.getId()。 equalsIgnoreCase(studentsMarkers.get(i).getId())){
studentsMarkers.get(i).remove();
stdId.remove(i);
final String id = stdId.get(i);
HashMap< String,Object> result = new HashMap<>();
result.put(show,false);
FirebaseDatabase.getInstance()。getReference()。child(Students)。child(id)
.updateChildren(result);
}
}
}

//我用这个来显示或者隐藏所有的标记..它工作正常没有问题在这里
void updateShowing (final boolean show){

for(int i = 0; i< studentsMarkers.size(); i ++){
studentsMarkers.get(i).remove();

$ b $ FirebaseDatabase.getInstance()。getReference()。child(Students)
.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
)public void onDataChange(DataSnapshot snapshot){
for(DataSnapshot ds:snapshot.getChildren()){
ds.child(show)。getRef()。setValue(show);
} b

$ b @Override
public void onCancelled(DatabaseError error){
Toast.makeText(getActivity(),+ error.getMessage(), Toast.LENGTH_SHORT).show();
Log.i(onCancelled:,error.getDetails()+\\\
+ error.getMessage());
}
});


解决方案

您应该在您的onWindowInfoButtonClicked()方法。将此行 studentsMarkers.get(i).remove(); 更改为 studentsMarkers.get(i).setVisible(false);



另外,我不明白你的逻辑

  stdId卸下摆臂(ⅰ); 
final String id = stdId.get(i);

您删除了位置 i 的项目,但那么你试图在这个位置访问一个新项目 i 并更新它的值?



你可以简化你的方法 void onWindowInfoButtonClicked(Marker marker)如下。由于您已经将目标Marker对象传递给此函数,因此无需循环访问 studentMarkers 列表以获取标记对象。

  void onWindowInfoButtonClicked(Marker marker){
marker.setVisible(false);
String id = marker.getId();
HashMap< String,Object> result = new HashMap<>();
result.put(show,false);
FirebaseDatabase.getInstance()。getReference()。child(Students)。child(id)
.updateChildren(result);
}

以上方法只会隐藏标记但不会删除它,因为您的标题声明隐藏。

编辑

您可以设置onMarkerClick )而不是onWindowInfoButtonClicked(Marker marker)。

  public boolean onMarkerClick(final Marker marker){
marker.setVisible假);
String id = marker.getId();
HashMap< String,Object> result = new HashMap<>();
result.put(show,false);

FirebaseDatabase.getInstance()。getReference()。child(Students)。child(id)
.updateChildren(result);
返回false;

请记得设置您的地图以倾听此听众

  yourMap.setOnMarkerClickListener(this); 


So I have a bunch of markers on the map and they get their data from firebase real-time database. I have like a switch button on the map that shows or hides all markers and it works fine. But now I want specific marker to hide when some action occurred to it, but it's not working..!

What really happens is.. when I trigger some action on that marker.. it gets updated on the database normally but it did not hide from map in real-time.! I had to close the map and open it again to see the changes..!

Whats the problem..!?


The Code

   Marker studentMarker = null;
   ArrayList<Marker> studentsMarkers = new ArrayList<>();
   ArrayList<String> stdId = new ArrayList<>();


  @Override
  public void onChildChanged(DataSnapshot snapshot, String s) {
    LatLng latLng = new LatLng(snapshot.getValue(TestUserStudent.class).getLat(),
        snapshot.getValue(TestUserStudent.class).getLang());
    boolean isShow = snapshot.getValue(TestUserStudent.class).isShow();

    if (isShow) {
      studentMarker = googleMap.addMarker(new MarkerOptions()
          .position(latLng)
          .title(snapshot.getValue(TestUserStudent.class).getName() + "")
          .snippet(snapshot.getValue(TestUserStudent.class).getSection() + "")
          .icon(BitmapDescriptorFactory.fromBitmap(
              getMarkerBitmapFromView(snapshot.getValue(TestUserStudent.class).getImg(),
                  R.drawable.map_marker_red))));
      studentsMarkers.add(studentMarker);
      stdId.add(snapshot.getKey());
    } 
  }


   // this method called when I click on button inside the infowindow of a marker from the list and it updates the marker in the database correctly! 
   void onWindowInfoButtonClicked(Marker marker) {

    for (int i = 0; i < studentsMarkers.size(); i++) {
      if (marker.getId().equalsIgnoreCase(studentsMarkers.get(i).getId())) {
        studentsMarkers.get(i).remove();
        stdId.remove(i);
        final String id = stdId.get(i);
        HashMap<String, Object> result = new HashMap<>();
        result.put("show", false);
        FirebaseDatabase.getInstance().getReference().child("Students").child(id)
          .updateChildren(result);
      }
    }
  }

    // I use this to either show or hide all markers.. and it works fine no problem here
    void updateShowing(final boolean show) {

        for (int i = 0; i < studentsMarkers.size(); i++) {
          studentsMarkers.get(i).remove();
        }

    FirebaseDatabase.getInstance().getReference().child("Students")
        .addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot ds : snapshot.getChildren()) {
              ds.child("show").getRef().setValue(show);
            }
          }

          @Override
          public void onCancelled(DatabaseError error) {
            Toast.makeText(getActivity(), "" + error.getMessage(), Toast.LENGTH_SHORT).show();
            Log.i("onCancelled: ", error.getDetails() + "\n " + error.getMessage());
          }
        });
  }

解决方案

You should do it in your onWindowInfoButtonClicked() method. Change this line studentsMarkers.get(i).remove(); to studentsMarkers.get(i).setVisible(false);.

Also, I don't understand your logic of

stdId.remove(i);
final String id = stdId.get(i);

You removed the item at position i but then you try to access a new item at this position i and update its value?

And you may simplify your method void onWindowInfoButtonClicked(Marker marker) as below. Since you will pass in the targeted Marker object to this function already, there is no need to iterate through your studentMarkers list to get back the Marker object.

void onWindowInfoButtonClicked(Marker marker) {
    marker.setVisible(false);
    String id = marker.getId();
    HashMap<String, Object> result = new HashMap<>();
    result.put("show", false);
    FirebaseDatabase.getInstance().getReference().child("Students").child(id)
      .updateChildren(result);
}

The method above will only hide the marker but not delete it, since your title stated "Hiding".

EDIT

You can set onMarkerClick(Marker marker) instead of onWindowInfoButtonClicked(Marker marker).

public boolean onMarkerClick(final Marker marker) {
    marker.setVisible(false);
    String id = marker.getId();
    HashMap<String, Object> result = new HashMap<>();
    result.put("show", false);

    FirebaseDatabase.getInstance().getReference().child("Students").child(id)
      .updateChildren(result);
    return false;
}

Remember to set your map to listen to this listener

yourMap.setOnMarkerClickListener(this);

这篇关于从列表中隐藏一个标记不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆