2017-05-06 79 views
0

對於匹配的queryLocation,從未執行過android geofire onKeyEntered。但是onGeoQueryReady方法被執行。Android GeoFire onKeyEntered未觸發

所以當我點擊菜單底部欄時,addGeoQueryEventListener被執行並且onGeoQueryReady方法被執行。但是onKeyEntered方法從不執行。正如您在附件中看到的那樣,我將該位置與頂層節點放在同一個節點中。我還在數據庫引用中包含子路徑「xxx_location」。以下是我的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

+0

如果您在'onGeoQueryReady'被調用之前沒有鍵,那意味着沒有與您的查詢匹配的鍵。 –

+0

謝謝。正如你在屏幕截圖中看到的,我有三個名稱爲ID的節點,例如123有三個不同的位置,並且這些ID位於並行節點下面,仍然未被執行。我一直在努力數週,沒有運氣 – user1439582

+0

是的,這看起來不像是用Geofire編寫的位置。您缺少'g'鍵,這是Geofire用於查詢的幾何關係。 –

回答

2

你的JSON缺少g鍵,這是Geofire使用查詢geohashes。如果沒有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