在GeoFire节点下存储额外的数据 [英] Storing Extra Data Under GeoFire Nodes

查看:166
本文介绍了在GeoFire节点下存储额外的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过GeoFire在我的数据库中存储位置数据,这里是结构:

$ ge $ $ $ $ $ ge $ $ $ $
-Kdrel2Z_xWI280XNfGg
-g:dr5regw90s
-l
-0:40.7127837
-1:-74.00594130000002

可以将位置信息和geofire数据存储在不同的节点中,但是我发现存储 一些 这些geofire节点下的额外数据,例如业务名称。通过这种方式,我只需要打电话给我的Firebase,以获取附近的位置以及他们的名字。



这是可以通过 key_entered 方法?有没有人创建类似的解决方案?这真的是一个想法,即使位置信息不断更新吗?



任何输入赞赏!

解决方案

首先,您使用的应用程序结构是错误的。



让我们以用户应用程序为例



当您使用Geofire时,您有两个数据清单:

 每个用户的用户详细信息
a每个用户的经度和纬度坐标列表

试图存储在这样的结构中

 users:{
< userId> :{
userData:userData,
< geofireData>
...


code
$ b

试图存储userdata和geofiredata是一个坏主意,因为你主要是静态数据(你的用户的属性)与高度不稳定的数据(地理位置信息)混合。分离出来的两个导致更好的性能,这就是为什么Geofire执行它。



这就是为什么当你尝试向用户节点添加地理位置数据时,它会覆盖该节点以前的用户详细信息。

您的数据库结构应该是这样的。

 用户:{
< UserId> ; :{
UserData:UserData,
...
}
}
Users_location:{
< UserId> :{
< geofireData> ...






$ b

因此,对于创建相同的用户ID 2结构一个用户的详细信息,另一个用于该用户的地理定位详细信息。

如何推送用户并设置geofire数据。

  String userId = ref.child(users)。push()。getKey(); 

ref.child(users)。child(userId).setValue(user);

geoFire = new GeoFire(ref.child(user_location));
geoFire.setLocation(userId,new GeoLocation(lattitude,longitude));

您在地理定位中使用的userId与您在push()期间获得的相同。



因此,对于每个userId,您在anotehr结构中具有一个结构和位置细节的userdetails。

您需要在users_location节点上执行GeoQuery,然后在onKeyEntered方法上获取数据。我的例子中的参数键是userId。



$ p $ ge $ F $ users_location);
geoQuery = geoFire.queryAtLocation(geoLocation),radius);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener(){
@Override $ b $ public void onKeyEntered(String key,GeoLocation location){
//检索数据
//使用这个关键是userId从用户详细信息结构中获取用户详细信息


code


$ b <快乐编码:)

I am storing location data via GeoFire in my database, here is the structure:

geofire
  -Ke1uhoT3gpHR_VsehIv
  -Kdrel2Z_xWI280XNfGg
    -g: "dr5regw90s"
    -l
      -0: 40.7127837
      -1: -74.00594130000002

It is avised to store a location's information and geofire data in separate nodes, however I see an advantage to storing some extra data under these geofire nodes, such as the name of a business. This way I'd only have to make only one call to my Firebase to fetch nearby locations as well as their names.

Is this achievable via the key_entered method? Has anyone created a similar solution? Is this truly that bad of an idea even if the location info is consistently updated?

Any input is appreciated!

解决方案

Firstly the structure you are using for the app is wrong.

let us take an example of users app

When you use Geofire, you have two lists of data:

a list of user details for each user
a list of latitude and longitude coordinates for each user

You are trying to store both in same structure like this

"users" : {
    <userId> : {
        "userData" : "userData",
         <geofireData> 
        ...
    }
}

Trying to store userdata and geofiredata in one node is a bad idea, since you're mixing mostly static data (the properties of your user) with highly volatile data (the geo-location information).Separating the two out leads to better performance, which is why Geofire enforces it.

This is why when you try to add geolocation data to a user node it overwrites previous user details for that node.

Your database structure should be like this.

"Users" : {
    <UserId> : {
        "UserData" : "UserData",
        ...
    }
}
"Users_location" : {
    <UserId> : {
        <geofireData> ...
    }
}

Hence for the same user Id you create 2 structures one for user details and another for geolocation details of that user.

How to push the user and set the geofire data.

String userId = ref.child("users").push().getKey();

ref.child("users").child(userId).setValue(user);

geoFire = new GeoFire(ref.child("user_location"));
geoFire.setLocation(userId, new GeoLocation(lattitude, longitude));

the userId you use in geolocation is same as the one you got during push().

Hence for each userId you have userdetails in one structure and location details in anotehr structure.

To get the data, first you need to do GeoQuery at users_location node and then get the data on the onKeyEntered method. The parameter key is userId from my example.

geoFire=newGeoFire(FirebaseDatabase.getInstance().getReference().child("users_location");
geoQuery = geoFire.queryAtLocation(geoLocation), radius);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
    @Override
    public void onKeyEntered(String key, GeoLocation location) {
         //retrieve data
//use this key which is userId to fetch user details from user details structure
    }
};    

Happy coding :)

这篇关于在GeoFire节点下存储额外的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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