当前,我正在调用“属性”的引用,但同时我也想调用“地理围栏”的引用。是否可以在同一Java文件中执行此操作?我希望将来自属性和“地理围栏”的数据用于一个片段。其他解决方案将不胜感激。
vpblendvb
答案 0 :(得分:1)
根据您的评论,如果您需要从两个节点获取latitude
和longitude
并比较距离,则假设两个地方的两个属性均为double
和< strong>不是字符串,请使用以下代码行:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference geoFenceUidRef = rootRef.child("Geo-fence").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
double geoFenceLat = dataSnapshot.child("Latitude").getValue(Double.class);
double geoFenceLng = dataSnapshot.child("Longitude").getValue(Double.class);
Location geoFenceLocation = new Location("");
geoFenceLocation.setLatitude(geoFenceLat);
geoFenceLocation.setLongitude(geoFenceLng);
Log.d(TAG, geoFenceLat + ", " + geoFenceLng);
DatabaseReference propertyUidRef = rootRef.child("Property").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
double propertyLat = ds.child("latitude").getValue(Double.class);
double propertyLng = ds.child("longitude").getValue(Double.class);
Log.d(TAG, propertyLat + ", " + propertyLng);
//Compare
Location propertyLocation = new Location("");
propertyLocation.setLatitude(propertyLat);
propertyLocation.setLongitude(propertyLng);
float distance = geoFenceLocation.distanceTo(propertyLocation);
Log.d(TAG, String.valueOf(distance));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
propertyUidRef.addListenerForSingleValueEvent(eventListener);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
geoFenceUidRef.addListenerForSingleValueEvent(valueEventListener);
如果将它们另存为字符串,请将上面的代码行更改为:
double geoFenceLat = Double.parseDouble(dataSnapshot.child("Latitude").getValue(String.class));
double propertyLng = Double.parseDouble(dataSnapshot.child("Longitude").getValue(String.class));
和
double propertyLat = Double.parseDouble(ds.child("latitude").getValue(String.class));
double propertyLng = Double.parseDouble(ds.child("longitude").getValue(String.class));