如何在Firebase数据库中保存GeoFire坐标以及其他项目? [英] How to save GeoFire coordinates along with other items in Firebase database?
问题描述
我正在开发一个应用程序,并在 FirebaseDatabase
中保存一些 Strings
像postedAtTime,postingBy,postedOnDate。我想将 GeoFire
坐标保存在所有上述字符串保存的同一个节点中,以便稍后我可以轻松地进行查询。
以下是我保存所有字符串的路径: databaseReferenceHRequests = firebaseDatabase.getReferenceFromUrl(https://appName-e1a35.firebaseio.com/requests/) ;
这就是我保存的方式:
//在onButtonClicked方法中:
postNewRequest(null,imageUID,MainActivity.userName.getText()。toString(),time,date,utcFormatDateTime,MainActivity.userEmail.getText()。toString ),geoFire);
//方法:
public void postNewRequest(Bitmap bitmap,String imageUIDh,String postedBy,String postedAtTime,String postedOnDate,String utcFormatDateTime,String userEmail,GeoFire geoFire){
HRequest hRequest = new HelpRequest(null,imageUIDh,postedBy,postedAtTime,postedOnDate,utcFormatDateTime,userEmail,geoFire);
databaseReferenceHRequests.push()。setValue(hRequest);
$ b $ p
$ b 以下是数据库中的保存方式:
我想要的是,我要保存GeoFire坐标在相同的节点,这是 -KLIoLUsI0SpQZGpV1h4
这里。这只是一个推式ID,它随机产生。
我试过这个参考: geoFire = new GeoFire(firebaseDatabase.getReferenceFromUrl https://appName-e1a35.firebaseio.com/requests/));
然后用上面的其他项目推送它。但是,这只保存了GeoFire坐标,而不是节点下的其他项: requests
。
是我的GeoFire引用,以便它与所有的数据一起保存在同一个节点?
这里出了什么问题?请让我知道。
解决方案 Frank的回答是正确的,但我想举个例子。
您的数据库结构应该是这样的。
items:{
< itemId> :{
someData:someData,
...
}
}
items_location:{
< itemId> :{
< geofireData> ...
$ / code $ / pre
要获取数据,首先需要在 items_location
节点上执行GeoQuery,然后获取 onKeyEntered
方法的数据。在我的例子中,参数 key
是 itemId
。
<$ p $ geoFire = new GeoFire(FirebaseDatabase.getInstance()。getReference()。child(items_location);
geoQuery = geoFire.queryAtLocation(geoLocation),radius);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener(){
@Override $ b $ public void onKeyEntered(String key,GeoLocation location){
//检索数据
}
};
希望这有帮助。 > 编辑
如何推送项目并设置geofire数据。
字符串itemId = ref.child(items)。push().getKey();
ref.child(items)。child(itemId).setValue(item);
geoFire = new GeoFire(ref.child(items_location));
geoFire.setLocation(itemId,new GeoLocation(lattitude,longitude));
pre
$ b 编辑将项目数据和geofire数据保存在一个API调用中
<$ p GeoHash geoHash = new GeoHash(new GeoLocation(latitude,longitude));
Map< String,Object> updates = new HashMap<>();
更新。 put(items /+ itemId,item);
updates.put(items_location /+ itemId +/ g,geoHash。 getGeoHashString());
updates.put(items_location /+ itemId +/ l,Arrays.asList(latitude,longitude));
ref.updateChildren(updates);
I'm developing an app and saving some Strings
like postedAtTime, postedBy, postedOnDate in FirebaseDatabase
. I want to save the GeoFire
coordinates in the same node in which all the above string are saved, so that later I can do query, easily.
Here's the path to which I'm saving all the strings: databaseReferenceHRequests = firebaseDatabase.getReferenceFromUrl("https://appName-e1a35.firebaseio.com/requests/");
This is how I'm saving it:
// in onButtonClicked method:
postNewRequest(null, imageUID, MainActivity.userName.getText().toString(), time, date, utcFormatDateTime, MainActivity.userEmail.getText().toString(), geoFire);
// the method:
public void postNewRequest(Bitmap bitmap, String imageUIDh, String postedBy, String postedAtTime, String postedOnDate, String utcFormatDateTime, String userEmail, GeoFire geoFire) {
HRequest hRequest = new HelpRequest(null, imageUIDh, postedBy, postedAtTime, postedOnDate, utcFormatDateTime, userEmail, geoFire);
databaseReferenceHRequests.push().setValue(hRequest);
}
Here's how it is getting saved in the database:
What I want is, I want to save the GeoFire coordinates in the same node, which is -KLIoLUsI0SpQZGpV1h4
here. This is just a push id and it gets generated randomly.
I tried it by giving this reference: geoFire = new GeoFire(firebaseDatabase.getReferenceFromUrl("https://appName-e1a35.firebaseio.com/requests/"));
and then pushing it with other items as shown above. But, this saved only GeoFire coordinates and not the other items under the node: requests
.
So, what should be my GeoFire reference so that it gets saved along with all the data in the same node?
What is going wrong here? Please let me know.
Frank's answer is correct, but I want to give an example. Your database structure should be like this.
"items" : {
<itemId> : {
"someData" : "someData",
...
}
}
"items_location" : {
<itemId> : {
<geofireData> ...
}
}
To get the data, first you need to do GeoQuery at items_location
node and then get the data on the onKeyEntered
method. The parameter key
is itemId
from my example.
geoFire = new GeoFire(FirebaseDatabase.getInstance().getReference().child("items_location");
geoQuery = geoFire.queryAtLocation(geoLocation), radius);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
//retrieve data
}
};
Hope this helps.
EDIT How to push the item and set the geofire data.
String itemId = ref.child("items").push().getKey();
ref.child("items").child(itemId).setValue(item);
geoFire = new GeoFire(ref.child("items_location"));
geoFire.setLocation(itemId, new GeoLocation(lattitude, longitude));
EDIT Save the item data and geofire data in one API call
GeoHash geoHash = new GeoHash(new GeoLocation(latitude, longitude));
Map<String, Object> updates = new HashMap<>();
updates.put("items/" + itemId, item);
updates.put("items_location/" + itemId + "/g", geoHash.getGeoHashString());
updates.put("items_location/" + itemId + "/l", Arrays.asList(latitude, longitude));
ref.updateChildren(updates);
这篇关于如何在Firebase数据库中保存GeoFire坐标以及其他项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!