Search code examples
androidgeolocation

Why cannot get the distance using both location which fetch from firebase realtime database


I am new in Java and Firebase. Now I have try to using the geolocation of two location that get from database and calculate the distance in km using the geolocation formula. The coding as shown below.

 private void getUserLocation() {
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("UserID")
                .child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("User location");

        ValueEventListener listener = databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                userlatitude = dataSnapshot.child("latitude").getValue(String.class);
                userlongitude = dataSnapshot.child("longitude").getValue(String.class);

                Double UserLatitude = Double.parseDouble(userlatitude);
                Double UserLongitude = Double.parseDouble(userlongitude);

                //String Userlatitudedouble = String.valueOf(UserLatitude);
                //String Userlongitudedouble = String.valueOf(UserLongitude);

                Log.i("user double latitude", userlatitude);
                Log.i("User double longitude", userlongitude);

                Double hostlatitudedouble = Double.parseDouble(latitude_host);
                Double hostlongitudedouble = Double.parseDouble(longitude_host);

                //String hostlatitudedouble = String.valueOf(HostLatitude);
                //String hostlongitudedouble = String.valueOf(HostLongitude);

                Log.i("host double latitude", latitude_host);
                Log.i("host double longitude", longitude_host);

                getDistance(UserLatitude, UserLongitude, hostlatitudedouble, hostlongitudedouble);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

    private double rad2deg(double distance) {
        return (distance * 180.0/Math.PI);
    }

    private double deg2rad(double lat1) {
        return (lat1 * Math.PI / 180.0);
    }

    private void getDistance(double lat1, double long1, double lat2, double long2) {
        double longdiff = long1 - long2;
        double distance = Math.sin(deg2rad(lat1))
                * Math.sin(deg2rad(lat2))
                + Math.cos(deg2rad(lat1))
                * Math.cos(deg2rad(lat2))
                + Math.cos(deg2rad(longdiff));
        distance = Math.acos(distance);
        String testing= String.valueOf(distance);
        Log.i("testing0" , testing);

        distance = rad2deg(distance);

        distance = distance *60 * 1.1515 * 1.609344;      //distance in km
        String distances = String.valueOf(distance);
        Log.i("distance = ", distances);
        Toast.makeText(this, "Distance = " + distances, Toast.LENGTH_SHORT).show();
    }

Now I am sure that I am success to get the latitude and longitude of two places from Firebase. However, I dont know why I keep get the result of NaN for the calculation. Guys, please help.


Solution

  • use this function for calculating the distance of two location. I used that in my last project and I'm sure about that:

    public static double calculateDistance(LatLng pointA, LatLng pointB) {
        double earthRadius = 6371;
        double latDiff = Math.toRadians(pointB.getLatitude() - pointA.getLatitude());
        double lngDiff = Math.toRadians(pointB.getLongitude() - pointA.getLongitude());
        double a = Math.sin(latDiff / 2) * Math.sin(latDiff / 2) +
                Math.cos(Math.toRadians(pointA.getLatitude())) *
                        Math.cos(Math.toRadians(pointB.getLatitude())) *
                        Math.sin(lngDiff / 2) * Math.sin(lngDiff / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = earthRadius * c;
        int meterConversion = 1000;
        return distance * meterConversion;
    }