Unity3D位置数据不准确

时间:2018-03-28 20:08:03

标签: c# unity3d geolocation trigonometry

我正在尝试计算应用程序中的距离我在Unity中进行原型设计。我已经实现了我在网上找到的几个C#版本的Harvestine方程式,它们的行为完全一样......

public static float Haversine(float lat1, float lon1, float lat2, float lon2) {
    float R = 6372.8f; // In kilometers
    float dLat = toRadians(lat2 - lat1);
    float dLon = toRadians(lon2 - lon1);
    lat1 = toRadians(lat1);
    lat2 = toRadians(lat2);

    float a = Mathf.Sin(dLat / 2) * Mathf.Sin(dLat / 2) + Mathf.Sin(dLon / 2) * Mathf.Sin(dLon / 2) * Mathf.Cos(lat1) * Mathf.Cos(lat2);
    float c = 2 * Mathf.Asin(Mathf.Sqrt(a));
    return R * 2 * Mathf.Asin(Mathf.Sqrt(a));
}

public static float toRadians(float angle) {
    return Mathf.PI * angle / 180f;
}

这是我正在使用的Haversine C#功能......

但由于某种原因,没有任何变量变化,我的总距离不断增加。 请注意,此函数每250ms调用一次,然后输出另一个var用于测试以存储总行进距离,如下所示:

float Distance = Haversine (PreviousLocation.Latitude, RecentLocation.Latitude, PreviousLocation.Longitude, RecentLocation.Longitude);

TotalDistanceTravelled = TotalDistanceTravelled + Distance;

RecentLocation和PreviousLocation来自var存储位置历史记录,此数据与下面视频中显示的相同

这是我得到的输出,请注意编辑器中不是这种情况,仅在构建中:

https://youtu.be/5B--WWJHOYE

任何帮助解释这一点或建议一些更好的代码将不胜感激! (我有预感它可能与半径有关吗?)

1 个答案:

答案 0 :(得分:2)

您正在以错误的顺序将变量输入函数(或使用它们)。

您目前有:

float Distance = Haversine (PreviousLocation.Latitude, RecentLocation.Latitude, PreviousLocation.Longitude, RecentLocation.Longitude);

然而,根据你如何使用变量,我猜你的意思是:

float Distance = Haversine (PreviousLocation.Latitude, PreviousLocation.Longitude, RecentLocation.Latitude, RecentLocation.Longitude);