通过Firebase实时控制标记的可见性 [英] Controlling marker visibility in real-time via 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){
}
});
如何更新标记 TestUserStudent。 class Firebase rtd 您必须在更新标记前删除标记。创建 然后使用这样的列表: 然后定义一个方法来删除标记: 最后这样做: So I have students showing in the map and each one has I have a switch button that shows and hide the marker by updating the The problem is, when the value is set to Any suggestion.!? How I show the markers
How I update the markers
TestUserStudent.class
Firebase rtd you have to remove markers before updating markers.Create a Then use list like this: Then define a method to remove markers: At last do this:
这篇关于通过Firebase实时控制标记的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
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());
}
});
}
public class TestUserStudent {
boolean show;
//其他属性
public boolean isShow(){return show; }
public void setShow(boolean show){this.show = show; }
//其他setter& getters
}
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());
}
});
}
show
boolean value. I'm using this value to determine if the marker should show or not on the map using setVisible()
method.!show
value in the real-time database, and it doesn't always work. 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.!
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) {
}
});
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());
}
});
}
public class TestUserStudent {
boolean show;
//other attributes
public boolean isShow() { return show; }
public void setShow(boolean show) { this.show = show; }
//other setters & getters
}
ArrayList
to save Markers
references.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());
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);
}
}
private void removeStudentMarkers()
{
for(int i=0;i<studentMakersList.size();i++)
{
studentMarkersList.get(i).remove();
}
}
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());
}
});
}