【问题标题】:Firebase GeoFire, retrieve posts with a specific radius from my current positionFirebase GeoFire,从我当前位置检索具有特定半径的帖子
【发布时间】:2018-09-22 14:28:52
【问题描述】:

是否可以从我当前位置获取存储在具有特定半径的 firebase 数据库中的所有 GeoFire 帖子?

示例:如果进入我的数据库节点,我有 10 个帖子,并且只有 3 个从我当前位置有效,半径为 0.5 公里,我将获得 3 个帖子。

这是我的数据库的结构:

{
  "posts" : {
    "-L9qTBD1WPgF0h2ungz-" : {
      "desc" : "lorem ipsum",
      "isActive" : true,
      "idGeofire" : "ID1GEOFIRE"
    },
    "-L9qTDImKE4HHgxH5YDs" : {
      "desc" : "lorem ipsum",
      "isActive" : true,
      "idGeofire" : "ID2GEOFIRE"
    }
  },
  "geofire" : {
    "ID1GEOFIRE" : {
      "IdPost" : "L9qTBD1WPgF0h2ungz",
      "l" : {
        "0" : 46.632347,
        "1" : 3.6354347
      }
    },
    "ID2GEOFIRE" : {
      "IdPost" : "L9qTDImKE4HHgxH5YDs",
      "l" : {
        "0" : 25.6314647,
        "1" : 50.6369447
      }
    },
  }
}

【问题讨论】:

    标签: android firebase firebase-realtime-database geolocation geofire


    【解决方案1】:

    要获取某个位置特定范围内的节点,您将use a geoquery 如文档所示:

    // creates a new query around [37.7832, -122.4056] with a radius of 0.6 kilometers
    GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(37.7832, -122.4056), 0.6);
    

    然后您将收到范围内节点的onKey... 事件,例如对于每个帖子,您都会得到一个onKeyEntered

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      相关资源
      最近更新 更多