2017-07-17 73 views
0

所以我在我的火力數據庫的多個位置: Database Structure GeoFire查詢顯示所有標記

寫了這樣遍歷所有這些節點,提取的經度和緯度,然後顯示它們作爲標記。

final DatabaseReference ref = FirebaseDatabase.getInstance().getReference("locations"); 
    final GeoFire geoFire = new GeoFire(ref); 

    //Add a location 
    //geoFire.setLocation("meetFar2", new GeoLocation(26.173027, -80.252871)); 

    final GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(userLocation.latitude, userLocation.longitude), 5.7); 
    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)); 
     Query locationQuery = FirebaseDatabase.getInstance().getReference().child("locations"); 
     locationQuery.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot snap : dataSnapshot.getChildren()){ 
       Double latitude = (Double) snap.child("l/0").getValue(); 
       Double longitude = (Double) snap.child("l/1").getValue(); 
       LatLng meetLatLng = new LatLng(latitude, longitude); 
       googleMap.addMarker(new MarkerOptions().position(meetLatLng)); 
      } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
     } 

     @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() { 
     } 

     @Override 
     public void onGeoQueryError(DatabaseError error) { 
     Toast.makeText(MainActivity.this, "There was an error with this query " + error, Toast.LENGTH_SHORT).show(); 
     } 
    }); 

這是內onMapReady全部完成,我得到了用戶的最後已知位置(設置爲用戶位置)之後。但是,當我運行該程序時,如果在查詢的半徑中至少有一個鍵,則地圖顯示所有標記。我如何才能將查詢中的結果顯示在半徑內作爲標記?

回答

0

認識到初始queryAtLocation查詢從我的數據庫中返回了正確數量的點,所以我刪除了其中的第二個查詢。代碼現在看起來像這樣:

final GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(userLocation.latitude, userLocation.longitude), 7.0); 
    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)); 

     LatLng meetLatLng = new LatLng(location.latitude, location.longitude); 
     googleMap.addMarker(new MarkerOptions().position(meetLatLng)); 
     }