通过Firebase实时控制标记的可见性 [英] Controlling marker visibility in real-time via Firebase

查看:122
本文介绍了通过Firebase实时控制标记的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我让学生在地图上显示,每个人都有 show 布尔值。我使用这个值来确定标记是否应该使用 setVisible()方法在地图上显示。!



我有一个切换按钮,通过更新实时数据库中的 show 值来显示和隐藏标记,但它并不总是有效。



问题是,当值设置为 false =>标记不会隐藏!!,但是当它变成 true 时,它们显示出来了!标记隐藏的唯一方法是如果我关闭地图然后再次打开它,这当然不是任何人想要的。



任何建议。






我如何显示标记

  FirebaseDatabase database = FirebaseDatabase.getInstance(); 
DatabaseReference refStudents = database.getReference(Students);
refStudents.addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded(DataSnapshot snapshot,String s){
}

@Override
public void onChildChanged(DataSnapshot snapshot,String s){

LatLng latLng = new LatLng(snapshot.getValue(TestUserStudent.class).getLat(),
snapshot.getValue(TestUserStudent ();
boolean show = snapshot.getValue b .position(latLng)
.title(item.getValue(TestUserStudent.class).getName()+)
.snippet(item.getValue(TestUserStudent.class).getSection()+ )
.icon(BitmapDescriptorFactory.fromBitmap(
getMarkerBitmapFromView(item.getValue(TestUserStudent.class)
.getImg(),R.drawable.redMarker))));

if(show ){
studentMarker.setVisible(true);
} else {
studentMarker.setVisible(false);

$ b}

@Override
public void onChildRemoved(DataSnapshot snapshot){
}

@覆盖
public void onChildMoved(DataSnapshot snapshot,String s){
}

@Override
public void onCancelled(DatabaseError error){
}
});






如何更新标记

pre $ private void updateShowing(final boolean isShow){

FirebaseDatabase.getInstance()。getReference ()DataSnapshot ds:snapshot。
.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot snapshot){
) getChildren()){
ds.child(show)。getRef()。setValue(isShow);
}
}
$ b $ @覆盖
public void onCancelled(DatabaseError error){
Log.i(onCancelled:,error.getDetails()+\\\
+ error.getMessage());
}
});
}






TestUserStudent。 class

  public class TestUserStudent {

boolean show;
//其他属性

public boolean isShow(){return show; }
public void setShow(boolean show){this.show = show; }

//其他setter& getters
}






Firebase rtd



解决方案

您必须在更新标记前删除标记。创建 ArrayList 以保存标记引用。

  ArrayList< Marker> studentMarkersList = new ArrayList(); 

然后使用这样的列表:

 @Override 
public void onChildChanged(DataSnapshot snapshot,String s){

LatLng latLng = new LatLng(snapshot.getValue(TestUserStudent.class).getLat (),
snapshot.getValue(TestUserStudent.class).getLang());
布尔show = snapshot.getValue(TestUserStudent.class).isShow();

if(show)
{
studentMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title(item.getValue (TestUserStudent.class).getName()+)
.snippet(item.getValue(TestUserStudent.class).getSection()+)
.icon(BitmapDescriptorFactory.fromBitmap(
getMarkerBitmapFromView(item.getValue(TestUserStudent.class)
.getImg(),R.drawable.redMarker))));
studentMarkersList.add(studentMarker);
}



}

然后定义一个方法来删除标记:

  private void removeStudentMarkers()
{
for(int i = 0; i< studentMakersList.size(); i ++)
{
studentMarkersList.get(i).remove();


$ / code $ / pre

最后这样做:

  private void updateShowing(final boolean isShow){

removeStudentMarkers();

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


@Override
public void onCancelled(DatabaseError error){
Log.i(onCancelled:,error.getDetails()+\\\
+ error .getMessage());
}
});
}


So I have students showing in the map and each one has show boolean value. I'm using this value to determine if the marker should show or not on the map using setVisible() method.!

I have a switch button that shows and hide the marker by updating the show value in the real-time database, and it doesn't always work.

The problem is, when the value is set to false => the markers don't hide!!, but when it becomes true they show up !? the only way for the markes to hide is if I close the map and then open it again which of course is not what anyone want.!

Any suggestion.!?


How I show the markers

FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference refStudents = database.getReference("Students");
    refStudents.addChildEventListener(new ChildEventListener() {
      @Override
      public void onChildAdded(DataSnapshot snapshot, String s) {
      }

      @Override
      public void onChildChanged(DataSnapshot snapshot, String s) {

       LatLng latLng = new LatLng(snapshot.getValue(TestUserStudent.class).getLat(), 
       snapshot.getValue(TestUserStudent.class).getLang());
       boolean show = snapshot.getValue(TestUserStudent.class).isShow();

    studentMarker = googleMap.addMarker(new MarkerOptions()
            .position(latLng)
            .title(item.getValue(TestUserStudent.class).getName() + "")
            .snippet(item.getValue(TestUserStudent.class).getSection() + "")
            .icon(BitmapDescriptorFactory.fromBitmap(
             getMarkerBitmapFromView(item.getValue(TestUserStudent.class)
              .getImg(), R.drawable.redMarker))));

       if (show) {
      studentMarker.setVisible(true);
      }else {
      studentMarker.setVisible(false);
       }

      }

      @Override
      public void onChildRemoved(DataSnapshot snapshot) {
      }

      @Override
      public void onChildMoved(DataSnapshot snapshot, String s) {
      }

      @Override
      public void onCancelled(DatabaseError error) {
      }
    });


How I update the markers

private void updateShowing(final boolean isShow) {

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

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


TestUserStudent.class

public class TestUserStudent {

  boolean show;
  //other attributes 

  public boolean isShow() { return show; }
  public void setShow(boolean show) { this.show = show; }

 //other setters & getters
 }


Firebase rtd

解决方案

you have to remove markers before updating markers.Create a ArrayList to save Markers references.

ArrayList<Marker> studentMarkersList=new ArrayList();

Then use list like this:

@Override
      public void onChildChanged(DataSnapshot snapshot, String s) {

       LatLng latLng = new LatLng(snapshot.getValue(TestUserStudent.class).getLat(), 
       snapshot.getValue(TestUserStudent.class).getLang());
       boolean show = snapshot.getValue(TestUserStudent.class).isShow();

       if(show)
        {
             studentMarker = googleMap.addMarker(new MarkerOptions()
            .position(latLng)
            .title(item.getValue(TestUserStudent.class).getName() + "")
            .snippet(item.getValue(TestUserStudent.class).getSection() + "")
            .icon(BitmapDescriptorFactory.fromBitmap(
             getMarkerBitmapFromView(item.getValue(TestUserStudent.class)
              .getImg(), R.drawable.redMarker))));
             studentMarkersList.add(studentMarker);
        }



      }

Then define a method to remove markers:

private void removeStudentMarkers()
            {
                for(int i=0;i<studentMakersList.size();i++)
                {
                    studentMarkersList.get(i).remove();
                }
            }

At last do this:

private void updateShowing(final boolean isShow) {

    removeStudentMarkers();

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

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

这篇关于通过Firebase实时控制标记的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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