0

使用 GPS 点,我通过使用 location1.distanceTo(location2) 保存先前和当前点来计算,方法是将每次时间添加到某个时间差异上的变量以获得行驶距离。获取车辆移动距离的好方法吗?有没有更好的方法来获得移动车辆期间的准确行驶距离?

4

1 回答 1

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);
于 2016-04-01T09:20:45.800 回答