Home > OS >  Constantly calculate the distance between my position and coordinates taken from a firebase realtime
Constantly calculate the distance between my position and coordinates taken from a firebase realtime

Time:01-21

In the application I am trying to create you can only join a guild if the guild leader is no more than 2km from my current position. I have a realtime database in which I have memorized the coordinates of the various leaders of the guilds. How do I calculate the distance between my position and the coordinates of the leader so that I can join the guild?

This is an image of my realtime database structure

I use an Interface for get the name of the guild and then the coordinates of the leader.

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());
        }
    });

}

Inside the 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());
            }
        });

    }
});

My onMapReady(), here i get my current position.

@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));
    }
}

At this point how can I calculate the distance between the coordinates taken from the database and my current location? (Inside onLocationChanged()?)

CodePudding user response:

Since you have no way of knowing which happens first (loading from the database, or the location change listener), you'll have to check whether you have all data in both callbacks.

I recommend:

  1. Creating two new fields in your activity class: one for the location fro the database, and one for the location from the phone itself.

  2. Create a new method in the activity class (or elsewhere) that checks whether both location fields are set, and if they are within the range you care about.

  3. Then call that new method from onDataChange and from onLocationChanged.

  •  Tags:  
  • Related