安卓:distanceTo()错误的价值观 [英] Android: distanceTo() wrong values

查看:216
本文介绍了安卓:distanceTo()错误的价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写我的跟踪路线的应用程序。
我请求从GPS每分钟更新和正常工作。
它显示了我,我的确切点。
当我想目前点previous之一,它工作正常,但形式之间的时间来计算距离,计算准确计算距离完全以错误的(我移动了200米和它重新调整了我的价值超过10公里)。
没有人知道为什么会发生这种情况?

I am writing an application for tracking my route. I am requesting updates from GPS every minute and it works fine. It shows me my exact point. When I want to calculate distance between current point and previous one it works ok, but form time to time it calculates distance totaly wrong (I moved about 200m and it retuned me a value over 10km). Does anyone know why this could happen?

下面是我使用的功能:

iRoute += myGPSLocation.distanceTo(prevLocation);

在此先感谢!

推荐答案

停止使用谷歌的Location.distancebetween&安培; location.distanceto功能。他们没有始终如一地工作。

Stop using google's Location.distancebetween & location.distanceto functions. They don't work consistently.

而不是使用直接的公式来计算距离:

Instead use the direct formula to calculate the distance:

double distance_between(Location l1, Location l2)
{
    //float results[] = new float[1];
    /* Doesn't work. returns inconsistent results
    Location.distanceBetween(
            l1.getLatitude(),
            l1.getLongitude(),
            l2.getLatitude(),
            l2.getLongitude(),
            results);
            */
    double lat1=l1.getLatitude();
    double lon1=l1.getLongitude();
    double lat2=l2.getLatitude();
    double lon2=l2.getLongitude();
    double R = 6371; // km
    double dLat = (lat2-lat1)*Math.PI/180;
    double dLon = (lon2-lon1)*Math.PI/180;
    lat1 = lat1*Math.PI/180;
    lat2 = lat2*Math.PI/180;

    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double d = R * c * 1000;

    log_write("dist betn "+
            d + " " +
            l1.getLatitude()+ " " +
            l1.getLongitude() + " " +
            l2.getLatitude() + " " +
            l2.getLongitude()
            );

    return d;
}

这篇关于安卓:distanceTo()错误的价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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