1

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

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

    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;

    }
4

1 回答 1

0

你可以只使用静态方法

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

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

于 2013-05-11T00:08:38.023 回答