Geoquery找出用户当前位置1 km范围内的app用户不能正常工作

时间:2017-07-25 10:03:19

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

我想检索用户当前位置1公里半径范围内存在的所有Android app用户的列表。下面是我正在尝试的代码,作为回报,它给了我自己的latlong。帮我弄清楚我错在哪里。

 double radius = 100;

 databaseReference = FirebaseDatabase.getInstance().getReference();
GeoFire geoFire = new GeoFire(databaseMediCare);
mAuth = FirebaseAuth.getInstance();
String uid = mAuth.getCurrentUser().getUid();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("userLocation");
final List<String> data = new ArrayList<>();
databaseReference .child("users").child(mAuth.getCurrentUser().getUid()).child("geocordinates").child("latitude").setValue(latitude);
databaseReference .child("users").child(mAuth.getCurrentUser().getUid()).child("geocordinates").child("longitude").setValue(longitude);
databaseReference .child("users").child(mAuth.getCurrentUser().getUid()).child("availability").setValue(true);
geoFire.setLocation(uid, new GeoLocation(latitude, latitude));
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(latitude, longitude), radius);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
    @Override
    public void onKeyEntered(String key, GeoLocation location) {
        //showToast("geocodes successfully found");
        data.add(key);
        Location itemLocation = new Location("itemLocation");
        itemLocation.setLatitude(location.latitude);
        itemLocation.setLongitude(location.longitude);
       // Log.d("distance to " + key" is", userLocation.distanceTo(itemLocation) + "");
        Log.i("geocodes Number of users", String.valueOf(data.size()));
    }

        @Override
        public void onKeyExited(String key) {
            Log.i(key, "geocodes left the place");
            data.remove(key);
        }

        @Override
        public void onKeyMoved(String key, GeoLocation location) {
            Log.i(key, "geocodes key moved but here");
        }

        @Override
        public void onGeoQueryReady() {
            Log.i("geocodes","All initial data has been loaded and events have been fired!");
        }

        @Override
        public void onGeoQueryError(DatabaseError error) {
            Log.i("geocodes", "error occured");
        }

    });

1 个答案:

答案 0 :(得分:0)

您可以使用PlaceLikehood而不是Geoquery。 You can see at this link.

PlaceLikehood为您提供附近的地方。然后,您通过Location.distanceTo方法获得距离。