I can't get markers from firebase

穿精又带淫゛_ 提交于 2020-03-21 11:04:12

问题


When i start thist activity my app stopping.I need get coordinates from databes and place marker. I don't know where is problem.

My database:

public class MapsActivityUser extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
DatabaseReference f_database = FirebaseDatabase.getInstance().getReference();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps_user);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;
    f_database.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                Map data = (Map) child.getValue();
                Map mCoordinate = (HashMap)data.get("Blog");
                double latitude = (double) (mCoordinate.get("lat"));
                double longitude = (double) (mCoordinate.get("lng"));
                LatLng location = new LatLng(latitude,longitude);
                googleMap.addMarker(new MarkerOptions().position(location).title("pedik"));
            }
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

回答1:


Try like this to retrive lat and lng

f_database.child("Blog").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot: dataSnapshot.getChildren()) {         
                double latitude = (double) (snapshot.child("lat").getValue());
                double longitude = (double) ((snapshot.child("lng").getValue());
                LatLng location = new LatLng(latitude,longitude);
                googleMap.addMarker(new MarkerOptions().position(location).title("pedik"));
            }
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });



回答2:


First, if you want to fetch all the Blog's children then you should put your reference on DatabaseReference f_database = FirebaseDatabase.getInstance().getReference("Blog");

When you put your firebase reference on DatabaseReference f_database = FirebaseDatabase.getInstance().getReference(). This will fetch all the nodes of the database, in your case Blog and users. If you need the info of blogs you don't need to fetch the data of users. This is a very poor code practice when it comes to firebase.

Secondly, as @Benjith binja suggested you can fetch all the Blog's children to fetch latitude & longitude. Alternatively, you should make a Blog Object and fetch the data. It is a good practice. https://firebase.google.com/docs/database/android/read-and-write

Blog Object:

class Blog {
    public String desc,id,image,time,title,uid,username;
    public Double lat,lng;

    public Blog(String desc, String id, String image, String time, String title, String uid, String username, Double lat, Double lng) {
        this.desc = desc;
        this.id = id;
        this.image = image;
        this.time = time;
        this.title = title;
        this.uid = uid;
        this.username = username;
        this.lat = lat;
        this.lng = lng;
    }
}

The firebase listener would look something like this:

DatabaseReference f_database = FirebaseDatabase.getInstance().getReference("Blog");
ValueEventListener listener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.exists()){
            for(DataSnapshot snapshot:dataSnapshot.getChildren()){
                // Get Blog object and use the values to update the UI
                Blog blog = snapshot.getValue(Blog.class);
                LatLng location = new LatLng(blog.lat,blog.lng);
                googleMap.addMarker(new MarkerOptions().position(location).title("pedik"));
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
};
f_database.addListenerForSingleValueEvent(listener);

Error must be occurring due to the wrong usage of Map. Here is how your code should look.

@Override
public void onMapReady(GoogleMap googleMap) {

mMap = googleMap;
f_database.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            Map data = (Map) child.getValue();
            Map mCoordinate = (HashMap)data.get("Blog");
            //mCoordinate is a map with values of all the children of Blog
            for(String key: mCoordinate.keySet()){
                Map mCoordinateChildMap = (HashMap) mCoordinate.get(key)
                double latitude = (double) (mCoordinateChildMap.get("lat"));
                double longitude = (double) (mCoordinateChildMap.get("lng"));
                LatLng location = new LatLng(latitude,longitude);
                googleMap.addMarker(new MarkerOptions().position(location).title("pedik"));   
            }
        }
    }


    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});


来源:https://stackoverflow.com/questions/48000566/i-cant-get-markers-from-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!