首页 > 解决方案 > 为什么无法使用从 firebase 实时数据库中获取的两个位置来获取距离

问题描述

我是 Java 和 Firebase 的新手。现在我尝试使用从数据库中获取的两个位置的地理位置,并使用地理位置公式计算以公里为单位的距离。编码如下图。

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

现在我确信我成功地从 Firebase 获得了两个地方的纬度和经度。但是,我不知道为什么我一直得到 NaN 的结果进行计算。伙计们,请帮忙。

标签: androidgeolocation

解决方案


使用此函数计算两个位置的距离。我在上一个项目中使用了它,我很确定:

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

推荐阅读