Android:使用移动设备计算车辆移动距离 [英] Android: calculate vehicle moving distance using mobile device

查看:114
本文介绍了Android:使用移动设备计算车辆移动距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用我正在计算的GPS点,方法是通过保留先前和当前点 location1.distanceTo(location2),方法是将每个时间加到某个时间差的变量上,以获得行驶距离.获取车辆移动距离的好方法吗?有什么更好的方法可以在行驶中获得准确的行驶距离?

Using GPS points I am calculating by holding previous and current points with location1.distanceTo(location2) by adding each time to a variable on some time diff to get traveled distance. Is it good approach to get vehicle movement distance? Is any better approach to get accurate travel distance during moving vehicle?

推荐答案

使用以下代码.希望对您有所帮助.

Use the following code. I hope it will help.

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);

这篇关于Android:使用移动设备计算车辆移动距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆