How to recover all data inside the key in Firebase

Asked

Viewed 1,340 times

0

I have this structure at Firebase. And I need to capture all the latitudes and longitudes that are inside each key, I did a test and it’s working if I put this data in a folder without the key it shows the marker on the map without problems, but just one, I want to capture and add all of them as markers on the map, How can I do it? for?

firebase database

DatabaseReference ref = 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");
            }
        }

3 answers

0


To solve your problem do the following:

Makes the declaration in global scope of these variables

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

Then just make change in 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 each id of its database contained in upload to collect the latitudes and longitudes, will also insert into your bookmark nomes if in the database.

I have a project that works with mapas and Firebase.

0

Try it this way:

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);

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 short of time to explain what the code does exactly, but basically it goes through your Ode with the logic that was applied, using a for. I used the addValueEventListener so that you can go through all the items instead of just one. You get the whole Node uploads and you’ll be able to go through his data.

  • Iamluc, thanks for the tip, but for me it didn’t work, is giving null error Object Reference in the two gets, it’s not finding the object, what’s wrong? if it is by my code above placing Child in an example folder I created called "up" where it contains only 1 latitude and longitude data it works, problem in key will be?

  • Using your code and using a Log. v("log",""+local); It captures all the data that has latitude and longitude and prints for me in the logcat. But after opening the map, where you should add the markers on the map, it closes the app and shows in the logcat this>> Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null Object Reference

  • Where are you putting the bookmark addition code?

  • I think the correct thing would be to open the map first and then take the data from firebase to add the markers on the map.

  • The marker addition code is in the code above the question, that’s it, I have the Showmap class that contains the map where it locates and points to my current location and after that the code above where it searches the Firebase data and should add the markers on the map, but does not end up doing, I ask you need get/set entity to perform this procedure, or that is wrong. And I have in the main a button that Intent to the map.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.