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

问题描述

因此,我让学生在地图上显示,每个人都有

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

  ArrayList< Marker> studentMarkersList = new ArrayList(); 

然后使用这样的列表:

 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实时控制标记的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 09:25
查看更多