我无法从firebase检索位置到我的应用。嗯,我做了,但只是一次。
我想从firebase检索所有用户的lat和lng,将标记放在谷歌地图上的这些坐标中。我该怎么办?
这是我的MapsActivity:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private DatabaseReference mDataBase;
@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.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
/* LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/
DatabaseReference ref = FirebaseDatabase
.getInstance()
.getReference()
.child("Estado")
.child("Cidade")
.child("Medianeira")
.child("Casa");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
double location_left = dataSnapshot.child("lat").getValue(Double.class);
double location_right = dataSnapshot.child("lng").getValue(Double.class);
LatLng sydney = new LatLng(location_left, location_right);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
//mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 24));
} else {
Toast.makeText(MapsActivity.this, "No data change", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}