从Firebase获取地图标记

时间:2017-12-15 17:32:18

标签: android google-maps firebase firebase-realtime-database

我需要获取数据库的坐标,并使用这些坐标将标记放在确切的位置。

我的数据库:

Picture of my database

我的代码:

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

@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.child("profile").getChildren()) {
                String rightLocation = child.child("lat").getValue().toString();
                String leftLocation = child.child("lng").getValue().toString();

                double location_left = Double.parseDouble(leftLocation);
                double location_right = Double.parseDouble(rightLocation);
                LatLng cod = new LatLng(location_left, location_right);
                googleMap.addMarker(new MarkerOptions().position(cod).title("pedik"));
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}}

1 个答案:

答案 0 :(得分:0)

编辑回答

for (DataSnapshot child : dataSnapshot.getChildren()){ 
   String lat = child.child("profile").child("lat").getValue().toString();
}