我正在开发一个位置跟踪程序,我正在使用Firebase使其工作,所以在这样做时我使用了数据快照,即addValueEventListener
并覆盖方法onDataChange
并应用了一个循环即为( DataSnapshot postSnapshot : dataSnapshot.getChildren()
)但是这个循环的内容没有被执行/遍历。附加代码。请帮忙。
DatabaseReference locations;
Double lat, lng;
private GoogleMap mMap;
private String email;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_tracking);
// 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);
locations = FirebaseDatabase.getInstance().getReference("Locations");
if(getIntent() != null){
email = getIntent().getStringExtra("email");
lat = getIntent().getDoubleExtra("lat", 0);
lng = getIntent().getDoubleExtra("lng", 0);
}
if(!TextUtils.isEmpty(email)){
// Toast.makeText(this, " location for this user ", Toast.LENGTH_SHORT).show();
loadLocationForThisUser(email);
}
}
private void loadLocationForThisUser(String email) {
Query user_location = locations.orderByChild("email").equalTo(email);
user_location.addValueEventListener(new ValueEventListener() {
//locations.addValueEventListener(new ValueEventListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(MapTracking.this, "ondatachange", Toast.LENGTH_SHORT).show();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Toast.makeText(MapTracking.this, "ondatachange1223", Toast.LENGTH_SHORT).show();
Tracking tracking = postSnapshot.getValue(Tracking.class);
Toast.makeText(MapTracking.this, tracking.getLat() + " friend's location " + tracking.getLng(), Toast.LENGTH_SHORT).show();
LatLng friendlocation = new LatLng(Double.parseDouble(tracking.getLat()),
Double.parseDouble(tracking.getLng()));
Location currentuser = new Location("");
currentuser.setLatitude(lat);
currentuser.setLongitude(lng);
Location friend = new Location("");
friend.setLatitude(Double.parseDouble(tracking.getLat()));
friend.setLongitude(Double.parseDouble(tracking.getLng()));
Toast.makeText(MapTracking.this, tracking.getLat() + " friend's location " + tracking.getLng(), Toast.LENGTH_SHORT).show();
mMap.clear();
distance(currentuser, friend);
mMap.addMarker(new MarkerOptions()
.position(friendlocation)
.title(tracking.getEmail())
.snippet("Distance" + new DecimalFormat("#.#").format((currentuser.distanceTo(friend)) / 1000) + " km ")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 12.0f));
}
LatLng current = new LatLng(lat, lng);
//Toast.makeText(MapTracking.this, current.latitude + " current location " + current.longitude, Toast.LENGTH_SHORT).show();
mMap.addMarker(new MarkerOptions().position(current).title(FirebaseAuth.getInstance().getCurrentUser().getEmail()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
数据库结构
答案 0 :(得分:0)
不幸的是,这可能是由于一个错字。您的简化数据库结构如下:
df1$col_new <- df1[1:3][cbind(seq_len(nrow(df1)), df1$ID)]
df1$col_new
#[1] 12 6 21
但是您正在将- Location
- pushId
- email: "someone@example.com"
- lat: "25.0"
- lon: "15.0"
- uid: userId
附加到ValueEventListener
节点,而该节点不存在,因此不会从数据库中检索到任何数据:
Locations
如果您将其更改为指向“位置”节点,则locations = FirebaseDatabase.getInstance().getReference("Locations");
方法应该有效:
onDataChange()