Android:使用移动设备计算车辆移动距离

时间:2016-04-01 07:10:56

标签: java android gps android-gps

使用GPS点我通过保持前一点和当前点来计算 location1.distanceTo(location2)通过在某个时间差异上添加每个时间来获得行进距离。获得车辆移动距离是否很好?在移动车辆期间,有更好的方法来获得准确的行驶距离吗?

1 个答案:

答案 0 :(得分:0)

使用以下代码。我希望它会有所帮助。

public double CalculationByDistance(LatLng StartP, LatLng EndP) {
    int Radius = 6371;// radius of earth in Km
    double lat1 = StartP.latitude;
    double lat2 = EndP.latitude;
    double lon1 = StartP.longitude;
    double lon2 = EndP.longitude;
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
            * Math.sin(dLon / 2);
    double c = 2 * Math.asin(Math.sqrt(a));
    double valueResult = Radius * c;
    double km = valueResult / 1;
    DecimalFormat newFormat = new DecimalFormat("####");
    int kmInDec = Integer.valueOf(newFormat.format(km));
    double meter = valueResult % 1000;
    int meterInDec = Integer.valueOf(newFormat.format(meter));
    Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
            + " Meter   " + meterInDec);

    return Radius * c;
}



float[] results = new float[1];
Location.distanceBetween(oldPosition.latitude, oldPosition.longitude,
            newPosition.latitude, newPosition.longitude, results);