我目前正在使用Firebase github example来创建一个用户注册并可以发布状态更新的应用。"我能够正确地做到这一点,但现在我想添加更多参数,比如用户位置(纬度和经度),但我不知道我会把它放在哪里。我不确定将在什么时候添加所有要添加的内容。这是我目前的代码:
NewPostActivity:
private DatabaseReference mDatabase;
private EditText mTextField;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_post);
mDatabase = FirebaseDatabase.getInstance().getReference();
mTextField = (EditText) findViewById(R.id.postText);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabSubmitPost);
assert fab != null;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
submitPost();
}
});
}
private void submitPost() {
final String text = mTextField.getText().toString();
if (TextUtils.isEmpty(text)) {
mTextField.setError("Required");
return;
}
final String userId = getUid();
mDatabase.child("users").child(userId).addListenerForSingleValueEvent new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
if (user == null) {
Toast.makeText(NewPostActivity.this, "Could not fetch user", Toast.LENGTH_SHORT).show();
} else {
writeNewPost(userId, user.username, text);
}
finish();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(NewPostActivity.this, databaseError.toString(), Toast.LENGTH_SHORT).show();
}
});
}
private void writeNewPost(String userId, String username, String text) {
String key = mDatabase.child("posts").push().getKey();
Post post = new Post(userId, username, text);
Map<String, Object> postValues = post.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + key, postValues);
mDatabase.updateChildren(childUpdates);
}
这是帖子文件:
public class Post {
public String uid;
public String author;
public String text;
public int likeCount = 0;
public Map<String, Boolean> likes = new HashMap<>();
public Post() {
}
public Post(String uid, String author, String text) {
this.uid = uid;
this.author = author;
this.text = text;
}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("uid", uid);
result.put("author", author);
result.put("text", text);
result.put("likeCount", likeCount);
result.put("likes", likes);
return result;
}
}
答案 0 :(得分:2)
为了在保存Latitude & Longitude
时添加Post
,您可以将这两个参数添加到现有类中,您需要修改Post
类构造函数以添加另外两个参数,例如:
public double lat,lng;
public Post(String uid, String author, String text,double lat,double lng) {
this.uid = uid;
this.author = author;
this.text = text;
this.lat = lat;
this.lng = lng;
}
然后在toMap
函数中,您可以将参数添加到result
,如:
result.put("lat", lat);
result.put("lng", lng);
因此,当您在writeNewPost
函数中调用构造函数时,可以更改此行:
Post post = new Post(userId, username, text);
到
Post post = new Post(userId, username, text, lat, lng);
现在,您的新帖子将与纬度和经度一起发布
我假设您已经拥有纬度和经度
<强>更新强>
如果您希望看到Posts
张贴在Location
内的Firebase query
,您可以执行此类操作,
在您要查询posts
的{{1}}中,您可以添加orderByChild
和equalTo
过滤器以与当前latitude & Longitude
进行比较,
如果你想根据一些距离过滤它们,那么你将不得不查询所有posts
然后在本地比较它们