获得2点之间的距离,并找出速度

时间:2013-05-10 23:51:51

标签: java android math gps

我原本以为这是微不足道的,但是在这一天结束时,我的大脑已经死了。

以米为单位的距离似乎有效,里程似乎被打破,速度似乎被打破了。 任何帮助将不胜感激。

    public static double distFromInMiles(double lat1, double lng1, double lat2, double lng2) 
    {
        double earthRadius = 3959;
        double dLat = Math.toRadians(lat2-lat1);
        double dLng = Math.toRadians(lng2-lng1);
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                   Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                   Math.sin(dLng/2) * Math.sin(dLng/2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        double dist = earthRadius * c;

        return dist;
    }

    public static double distFromInMeters(double lat1, double lng1, double lat2, double lng2) 
    {
        double earthRadius = 6378137;
        double dLat = Math.toRadians(lat2-lat1);
        double dLng = Math.toRadians(lng2-lng1);
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                   Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                   Math.sin(dLng/2) * Math.sin(dLng/2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        double dist = earthRadius * c;

        return dist;
    }

    public static double distFromInKiloMeters(double lat1, double lng1, double lat2, double lng2) 
    {
        double earthRadius = 6371;
        double dLat = Math.toRadians(lat2-lat1);
        double dLng = Math.toRadians(lng2-lng1);
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                   Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                   Math.sin(dLng/2) * Math.sin(dLng/2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        double dist = earthRadius * c;

        return dist;
    }


    protected void checkDistanceFromPOI(LatLng latLng)
    {

        long thistime = System.currentTimeMillis();
        long deltatime = lasttime - thistime;

        double hours = (double)deltatime / ((double)1000.0 * (double)60.0 * (double)60.0);

        String s;
        double distFromInMeters = LocationGPSServices.distFromInMeters(lastLocationPoint.latitude, lastLocationPoint.longitude, latLng.latitude, latLng.longitude);
        double distFromInMiles = LocationGPSServices.distFromInMiles(lastLocationPoint.latitude, lastLocationPoint.longitude, latLng.latitude, latLng.longitude);
        double speed = distFromInMiles/hours;
        double speed2 = distFromInMeters/hours;

        s = "*****************distance from last point \n"
                + " lat/lng 1 = " + lastLocationPoint.toString() + "\n"
                + " lat/lng 2 = "+ latLng.toString() + "\n"
                + " in meters = " + distFromInMeters + "} \n in miles = " + distFromInMiles + " }\n "
                + " elapsed time = " + deltatime + "secs \n"
                + " speed = " + speed2 + "meters per hour \n"
                + " speed = " + speed + "miles per hour";
        Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
        Writer.appendText(s, "gps.txt", getActivity());
        lasttime = thistime;

    }

1 个答案:

答案 0 :(得分:0)

您可以使用静态方法

Location.distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)  

您也可以在Location类中使用getSpeed ()方法。