Android GeoFire onKeyEntered未触发

时间:2017-05-06 18:18:26

标签: android firebase google-maps-android-api-2 geofire

永远不会为匹配queryLocation执行android geofire onKeyEntered。但是执行了GeoQueryReady方法。

所以当我点击菜单底栏时,会执行addGeoQueryEventListener并执行onGeoQueryReady方法。但是onKeyEntered方法永远不会被执行。正如您在附件中看到的那样,我在顶级节点所在的节点中占有一席之地。我还包括子路径" xxx_location"在DB参考中。以下是我的API版本。

Geofire 2.1.1'
firebase-core:9.0.2'

代码:

 ///geofire connection

  DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
  DatabaseReference refLowLevel = ref.child("xxxxx_location");
  if (refLowLevel!=null) {
      GeoFire geoFire = new GeoFire(refLowLevel);

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

      Log.i(TAG, "query-------->"+geoQuery);

      markers = new HashMap<String, Marker>();

      geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {

          @Override
          public void onKeyEntered(String key, GeoLocation location) {
              Log.i(TAG, "inside painter onkeynetered-------->");

              //Marker marker = map.addMarker(new MarkerOptions().position(new LatLng(location.latitude, location.longitude)));
              //markers.put(key, marker);

          }

          @Override
          public void onKeyExited(String key) {
              Log.i(TAG, "inside painter onkeyexited-------->");
          }

          @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() {
              Log.i(TAG, "inside painter onqueryready-------->");
          }

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

  }

enter image description here

1 个答案:

答案 0 :(得分:2)

您的JSON缺少g个键,这是Geofire用于查询的地理位置。如果没有g密钥,它就无法在任何查询中找到任何密钥。

有关如何使用Geofire在数据库中设置位置的示例,请参阅documentation

geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973));

如果您想知道setLocation的作用,请参阅其代码:https://github.com/firebase/geofire-java/blob/8385c3148f816807ea3e1873c5e3772d76856057/src/main/java/com/firebase/geofire/GeoFire.java#L162