问题描述
我正在使用 firebase 在 android 上设计一个基于位置的餐厅搜索应用程序.Firebase 上的餐厅类如下;
I am designing a location based restaurant search application on android with firebase. Restaurant class as follows on firebase;
restaurants
-key
address: "NYC"
city: "New York"
description: ""
id: 60000
latitude: 39.895107
longitude: 32.797819
name: "Pizza Store"
片段类
mainActivity.mDatabase.child("restaurants").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
System.out.println("There are " + snapshot.getChildrenCount() + " restaurans");
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Restaurant post = postSnapshot.getValue(Restaurant.class);
mapmap2.put(post.getName(), postSnapshot.getKey());
restaurants.add(post);
}
adapter = new RestaurantCardViewAdapter(getActivity(), restaurants, MapListFragment.this);
mRecyclerView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
现在,我可以列出所有餐厅.但我想查询用户位置.首先,我想列出用户位置附近的 20 家餐馆,当用户加载页面时,我想列出下 20 家最近的餐馆.我在互联网上做一些搜索.我找到了 GeoFire 库,但据我所知,它可以与旧版 firebase 一起使用.如何使用这些条件进行查询?
Right now, I can list all the restaurants. But I want to query on user location. Firstly I want to list 20 restaurants near to user location and when user loads page through I want to get next 20 nearest restaurants. I do some search on internet. I found GeoFire library but as I understand its working with the legacy version of firebase. How can I query with these criterias?
推荐答案
Firebase 查询只能按单个属性过滤/排序.由于位置由经度和纬度组成,因此按照您现在的格式,您无法根据位置进行查询.
Firebase Queries can only filter/order by a single property. Since a location consists of longitude and latitude, in the format you have right now, you cannot query based on location.
幸运的是,有一个名为 GeoFire 的附加库.这个库将经度和纬度组合成一个名为 geohash 的属性.基于此属性,它可以按到给定位置的距离进行过滤.
Luckily there is an add-on library called GeoFire. This library combines the longitude and latitude into a single property called a geohash. Based on this property it can filter by distance to a given location.
我建议您查看 Geofire,看看它是否适合您的用例.
I recommend you check out Geofire and see if it fits your use-case.
这篇关于Firebase 位置查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!