2017-07-17 77 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)); 
     }