我需要获取数据库的坐标,并使用这些坐标将标记放置在确切的位置。
我的数据库:
我的代码:
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) {
}
});
}}
编辑回答
for (DataSnapshot child : dataSnapshot.getChildren()){
String lat = child.child("profile").child("lat").getValue().toString();
}