简体   繁体   中英

How to retrieve data from Firebase to make multiple Markers on map Android

this is my firebase look like

firebase .

And this my maps activity :

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
    private GoogleMap mMap;
    private DatabaseReference mUsers;
    Marker marker;
    public static final String FIREBASE_URL = "https://xxx.firebaseio.com/";
    private Firebase firebase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
        Firebase.setAndroidContext(this);
        firebase = new Firebase(FIREBASE_URL);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

    @Override
    public void onMapReady(final GoogleMap googleMap) {
        firebase.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(com.firebase.client.DataSnapshot dataSnapshot) {
                for (com.firebase.client.DataSnapshot s : dataSnapshot.child("marker").getChildren()) {
                    String lat = s.child("lat").getValue().toString();
                    String lng = s.child("lng").getValue().toString();

                    double latitude = Double.parseDouble(lat);
                    double longitude = Double.parseDouble(lng);
                    LatLng loc = new LatLng(latitude, longitude);
                    googleMap.addMarker(new MarkerOptions().position(loc).title(s.child("nama").getValue().toString()));
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

    }
}

and the error says

E/MarketPageConfiguration: parse page configuration json error
    org.json.JSONException: No value for homeIndex

and when i run it, it doesn't show the marker and the location still get wrong. I think i name it wrong about the "child" and get value in it, but i have no idea how to figure it out.

(the url link is all right)

To solve this, please change the following line of code:

firebase = new Firebase(FIREBASE_URL);

to

firebase = new Firebase(FIREBASE_URL).child("marker");

You are missing a child from the tree, which is marker and which is mandatory be used in your reference as well.

Further more, remove the .child("marker") from the followig line of code:

for (com.firebase.client.DataSnapshot s : dataSnapshot.child("marker").getChildren())

Shoul only be:

for (com.firebase.client.DataSnapshot s : dataSnapshot.getChildren())

Also note, that you are using a very old version of Firebase and I recommend you update to the latest one.

I have tried with this one

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
//    public static final String FIREBASE_URL = "https://trans-k-1546744104547.firebaseio.com/";
    private FirebaseDatabase firebase;
    private DatabaseReference databaseReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        firebase = FirebaseDatabase.getInstance();
        databaseReference = firebase.getReference("marker");

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(final GoogleMap googleMap) {
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot child : dataSnapshot.getChildren()) {
                    String lat = child.child("lat").getValue().toString();
                    String lng = child.child("lng").getValue().toString();

                    double latitude = Double.parseDouble(lat);
                    double longitude = Double.parseDouble(lng);
                    LatLng loc = new LatLng(latitude, longitude);
                    googleMap.addMarker(new MarkerOptions().position(loc).title(child.child("nama").getValue().toString()));
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}

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