如何将 GeoFire 坐标与 Firebase 数据库中的其他项目一起保存? [英] How to save GeoFire coordinates along with other items in Firebase database?

查看:17
本文介绍了如何将 GeoFire 坐标与 Firebase 数据库中的其他项目一起保存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序并在 Firebase 数据库中保存一些字符串,例如 postedAtTimepostedBypostedOnDate.我想将 GeoFire 坐标保存在保存上述所有字符串的同一节点中,以便稍后我可以轻松进行查询.

I'm developing an app and saving some strings like postedAtTime, postedBy, postedOnDate in Firebase database. 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/");

这是我保存它的方式:

// 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:

我想要的是将 GeoFire 坐标保存在同一个节点中,这里是 -KLIoLUsI0SpQZGpV1h4.这只是一个推送 ID,它是随机生成的.

What I want is 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/"));

然后将它与其他项目一起推送,如上图所示.但是,这仅保存了 GeoFire 坐标,而不保存节点 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.

那么,我的 GeoFire 参考应该是什么,以便它与同一节点中的所有数据一起保存?

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 的回答是正确的,但我想举个例子.你的数据库结构应该是这样的.

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> ...
    }
  }
}

要获取数据,首先需要在items_location 节点进行GeoQuery,然后通过onKeyEntered 方法获取数据.参数 key 是我示例中的 itemId.

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
    }
};    

希望这会有所帮助.

编辑如何推送item和设置geofire数据.

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));

编辑在一个 API 调用中保存项目数据和 geofire 数据

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);

这篇关于如何将 GeoFire 坐标与 Firebase 数据库中的其他项目一起保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆