How to recover all the data inside the key in Firebase

0

I have this structure in Firebase. And I need to capture all the latitudes and longitudes that are inside each key, I did a test and it is working if I for this data in a folder without the key it appears the marker on the map without problems, but only one, I want to capture and add all as markers on the map, how can I do it? with a for?

DatabaseReferenceref=FirebaseDatabase.getInstance().getReference();ref.child("uploads").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            if (dataSnapshot.exists()) {

                double location_left = (dataSnapshot.child("latitude").getValue(Double.class));
                double location_right = Double.valueOf(dataSnapshot.child("longitude").getValue(Double.class));
                LatLng local = new LatLng(location_left, location_right);
                mMap.addMarker(new MarkerOptions().position(local).title("Novo Marcador"));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(local, 18));
            } else {
                Log.i("MeuLOG", "erro na captura");
            }
        }
    
asked by anonymous 03.08.2017 / 02:13

3 answers

0

To solve your problem, do the following:

Make the global scope declaration of these variables

private String nome;
private String latitude;
private String longitude;
Marker marcador;

Then just make changes to your method.

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

ref.child("uploads").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

    if (dataSnapshot.exists()) {

        for (DataSnapshot s : dataSnapshot.getChildren()) {

            nome = s.child("nome").getValue().toString();
            latitude = s.child("latitude").getValue().toString();
            longitude = s.child("longitude").getValue().toString();
            LatLng zoom = new LatLng(Double.valueOf(latitude), Double.valueOf(longitude));

            marcador = mMap.addMarker(new MarkerOptions()
                            .position(new LatLng(Double.valueOf(latitude), Double.valueOf(longitude)))
                            .title(nome));

            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(zoom, 18));

            } 
        } else {
            Log.i("MeuLOG", "erro na captura");
        }

    }
}

This function, in addition to traversing every% of% of its database contained in id to collect upload and latitudes , will also insert its longitudes in the database if it has in the database .

I have a project that works with nomes and mapas .

    
02.05.2018 / 03:08
0

Try the following

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

ref.child("uploads").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                double location_left = postSnapshot.child("latitude").getValue(Double.class);
                // resto do codigo
            }
        } else {
            Log.i("MeuLOG", "erro na captura");
        }
    }
}

A little bit of time to explain what the code does exactly, but basically it runs through your entire node with the logic that was applied, using a for . I've used addValueEventListener so you can scroll through all items instead of just one. You get all node uploads and you can go through your data.

    
03.08.2017 / 02:38
0

Try this:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("uploads");

    ValueEventListener listener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Postagem.removeAll(Postagem);
            for (DataSnapshot single: dataSnapshot.getChildren()){

                if (single.exists()) {

                        double location_left = (single.child("latitude").getValue(Double.class));
                        double location_right = Double.valueOf(single.child("longitude").getValue(Double.class));
                        LatLng local = new LatLng(location_left, location_right);
                        mMap.addMarker(new MarkerOptions().position(local).title("Novo Marcador"));
                        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(local, 18));
                } else {
                    Log.i("MeuLOG", "erro na captura");
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };
    ref.addValueEventListener(listener);
    
17.12.2017 / 13:16