Firebase位置查询 [英] Firebase Location Query

查看:219
本文介绍了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"

碎片类

Fragment class

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?

推荐答案

我在答案的帮助下解决了这个问题。我把一个名叫restaurant_locations的Geofire推到一个单独的孩子身上。当我插入餐馆时,我会单独推送位置。

I solved the problem with the help of answers. I push a seperate child for geofire named as "restaurant_locations". I push the locations seperately when I insert restaurants.

      GeoFire geoFire;
      String itemId = mDatabase.child("restaurants").push().getKey();
      mDatabase.child("restaurants").child(itemId).setValue(restaurant);

      geoFire = new GeoFire(mDatabase.child("restaurants_location"));
      geoFire.setLocation(itemId, new GeoLocation(Double.valueOf(rd.location.latitude), Double.valueOf(rd.location.longitude)));

然后,当我需要通过位置进行查询时,我使用geofire query。

Then when I need to query through locations I used geofire query.

    geoFire = new GeoFire(mDatabase.child("restaurants_location"));
    GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(39.896263, 32.799652), 1);

    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {
            System.out.println(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude));
            mDatabase.child("restaurants").child(key).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    Restaurant res= snapshot.getValue(Restaurant.class);
                }
                @Override
                public void onCancelled(DatabaseError firebaseError) {
                    System.out.println("The read failed: " + firebaseError.getMessage());
                }

                // ...
            });
        }

        @Override
        public void onKeyExited(String key) {
            System.out.println(String.format("Key %s is no longer in the search area", key));
        }

        @Override
        public void onKeyMoved(String key, GeoLocation location) {
            System.out.println(String.format("Key %s moved within the search area to [%f,%f]", key, location.latitude, location.longitude));
        }

        @Override
        public void onGeoQueryReady() {
            System.out.println("All initial data has been loaded and events have been fired!");
        }

        @Override
        public void onGeoQueryError(DatabaseError error) {
            System.err.println("There was an error with this query: " + error);
        }
    });

但是我仍然担心这是从数据库获取餐馆最有效的方式,因为我运行单独为餐厅根目录上的每个餐馆钥匙查询。有没有人知道更有效的方式,请让我们知道。

But I still worry about that is it the most efficient way of getting Restaurants from database, because I run query seperately for every single restaurant key on "restaurants" root. Is there anybody know more efficient way please let us know.

这篇关于Firebase位置查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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