2016-04-01 15 views
0

GPSポイントを使用しています。移動距離を取得するためにある時間差分の各時間を変数に加算することで、前と現在のポイントを location1.distanceTo(location2)車の移動距離を得るのは良いアプローチですか?移動中に正確な走行距離を得るためのよりよいアプローチはありますか?Android:モバイルデバイスを使用して車両の移動距離を計算する

+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); 
関連する問題