简体   繁体   中英

How can I access all users from database?

Im using firebase database to save my coordinates.

this is how my database looks like: Firebase数据库

I'm saving data by token:

        String token = FirebaseInstanceId.getInstance().getToken();
        if (token == null) return;
        databaseReference = FirebaseDatabase.getInstance().getReference(token);`

So I need to access coordinates and add marker to a map

I'm doing it in valueEventListener in onMapReady method:

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference(FirebaseInstanceId.getInstance().getToken());
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            double allLat = (double) dataSnapshot.child("lat").getValue();
            double allLng = (double) dataSnapshot.child("lng").getValue();

            mMap.addMarker(new MarkerOptions().position(new LatLng(allLat, allLng)).title("Пользователь"));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(MapsActivity.this, "Не найдены пользователи", Toast.LENGTH_SHORT).show();
        }
    });

but it set me only my marker, how I can access all users from my database?

You have to loop through your values, inside your onDataChange() :

for(DataSnapshot child : dataSnapshot.getChildren() ){
            //get your mulitple markers here
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM