不断计算我的位置和从 firebase 实时数据库中获取的坐标之间的距离?

Constantly calculate the distance between my position and coordinates taken from a firebase realtime database?

在我尝试创建的应用程序中,如果公会会长距离我当前位置不超过 2 公里,您只能加入公会。我有一个实时数据库,其中我已经记住了行会的各个领导者的坐标。如何计算自己的位置与团长坐标的距离,才能加入公会?

This is an image of my realtime database structure

我使用一个接口来获取公会的名称,然后是领导者的坐标。

private interface FirebaseCallback{
    void onCallback(String gScelta);
}

private void readData(FirebaseCallback firebaseCallback){
    database.child("Utenti").child(currentUser.getUid()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                String stringNomeGilda = (dataSnapshot.getValue().toString());
                firebaseCallback.onCallback(stringNomeGilda);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Log.i("Info", "Failed to read value.", error.toException());
        }
    });

}

在 onCreate 内部:

readData(new FirebaseCallback() {
    @Override
    public void onCallback(String gScelta) {

        Log.i("Info","Gilda scelta con Interface: "+gScelta);

        database.child("Gilde").child("Gilda"+gScelta).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                DbGilda dbGilda = snapshot.getValue(DbGilda.class);

                //HEARE I TAKE THE CORRECT X AND Y!!!

                Log.i("Info", "X CAPO: " + dbGilda.X+"  Y CAPO: "+dbGilda.Y);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Log.i("Info", "Failed to read value.", error.toException());
            }
        });

    }
});

我的onMapReady(),在这里我得到了我的当前位置。

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    locationManager= (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationListener=new LocationListener() {
        @Override
        public void onLocationChanged(@NonNull Location location) {
            mMap.clear();
            LatLng UserLocation = new LatLng(location.getLatitude(), location.getLongitude());
            mMap.addMarker(new MarkerOptions().position(UserLocation).title("LA TUA POSIZIONE")).setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon3));
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(UserLocation,18));

        }
    };
    if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
    }
    else{
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
        Location lastknownlocation= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        mMap.clear();
        LatLng UserLocation = new LatLng(lastknownlocation.getLatitude(), lastknownlocation.getLongitude());
        mMap.addMarker(new MarkerOptions().position(UserLocation).title("LA TUA POSIZIONE")).setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon3));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(UserLocation,12));
    }
}

此时如何计算从数据库中获取的坐标与我当前位置之间的距离? (在 onLocationChanged() 内部?)

由于您无法知道哪个先发生(从数据库加载或位置更改侦听器),因此您必须检查是否在两个回调中都有所有数据。

我推荐:

  1. 在您的 activity class 中创建两个新字段:一个用于数据库的位置,一个用于 phone 本身的位置。

  2. 在 activity class(或其他地方)中创建一个新方法,检查是否设置了两个位置字段,以及它们是否在您关心的范围内。

  3. 然后从 onDataChangeonLocationChanged.

    调用新方法