Unity3D位置数据不准确
Unity3D inaccurate location data
我正在尝试计算我在 Unity 中制作原型的应用程序的距离。我已经实现了我在网上找到的 Harvestine 方程的几个 C# 版本,它们的行为方式完全相同...
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# 函数...
但由于某种原因,在没有任何变量变化的情况下,我的总距离一直在增加。
请注意,此函数每 250 毫秒调用一次,它的输出然后另一个 var 用于测试以存储总行进距离,如下所示:
float Distance = Haversine (PreviousLocation.Latitude, RecentLocation.Latitude, PreviousLocation.Longitude, RecentLocation.Longitude);
TotalDistanceTravelled = TotalDistanceTravelled + Distance;
RecentLocation 和 PreviousLocation 来自一个存储位置历史的 var,此数据与下面视频中显示的相同
这是我得到的输出,请注意在编辑器中不是这种情况,只有在构建中:
任何帮助解释这个或建议一些更好的代码将不胜感激! (我有预感这可能与半径有关?)
您以错误的顺序将变量输入函数(或使用它们)。
您目前拥有:
float Distance = Haversine (PreviousLocation.Latitude, RecentLocation.Latitude, PreviousLocation.Longitude, RecentLocation.Longitude);
不过,根据您使用变量的方式,我猜您的意思是:
float Distance = Haversine (PreviousLocation.Latitude, PreviousLocation.Longitude, RecentLocation.Latitude, RecentLocation.Longitude);
我正在尝试计算我在 Unity 中制作原型的应用程序的距离。我已经实现了我在网上找到的 Harvestine 方程的几个 C# 版本,它们的行为方式完全相同...
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# 函数...
但由于某种原因,在没有任何变量变化的情况下,我的总距离一直在增加。 请注意,此函数每 250 毫秒调用一次,它的输出然后另一个 var 用于测试以存储总行进距离,如下所示:
float Distance = Haversine (PreviousLocation.Latitude, RecentLocation.Latitude, PreviousLocation.Longitude, RecentLocation.Longitude);
TotalDistanceTravelled = TotalDistanceTravelled + Distance;
RecentLocation 和 PreviousLocation 来自一个存储位置历史的 var,此数据与下面视频中显示的相同
这是我得到的输出,请注意在编辑器中不是这种情况,只有在构建中:
任何帮助解释这个或建议一些更好的代码将不胜感激! (我有预感这可能与半径有关?)
您以错误的顺序将变量输入函数(或使用它们)。
您目前拥有:
float Distance = Haversine (PreviousLocation.Latitude, RecentLocation.Latitude, PreviousLocation.Longitude, RecentLocation.Longitude);
不过,根据您使用变量的方式,我猜您的意思是:
float Distance = Haversine (PreviousLocation.Latitude, PreviousLocation.Longitude, RecentLocation.Latitude, RecentLocation.Longitude);