【发布时间】:2025-12-18 21:10:03
【问题描述】:
我的代码在印度运行良好。此代码计算两个位置之间的大致距离。我使用了下面提到的代码。但是如果我在美国运行同样的代码。然后为每个位置更新计算不同的距离。 我将源和目的地的纬度和经度传递给了这个函数。该应用程序在印度运行良好。但不适用于美国。在同一个地方它显示不同的计算。 提前致谢。任何帮助将不胜感激。
public static double distanceKm(double lat1, double lon1, double lat2, double lon2) {
double lat1Rad = Math.toRadians(lat1);
double lat2Rad = Math.toRadians(lat2);
double deltaLonRad = Math.toRadians(lon2 - lon1);
return Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad) * Math.cos(lat2Rad)
* Math.cos(deltaLonRad))
* 6371;
}
【问题讨论】:
-
尝试双距离 = FIRSTLOCATION.distanceTo(SECONDLOCATION);
-
这个公式可能存在已知的限制。你能解释一下吗? b/ 对于印度和美国的一些示例测量,您的预期与实际结果是什么。
-
(另外,有内置的android方法可以做到这一点)
-
@njzk2 感谢您的快速回复。详细信息是印度 CurrentLat :18.50849876 CurrentLon:73.79455861 我家 dist:743 下一个位置已更改 : CurrentLat :18.50846096 CurrentLon:73.79453276 我家 dist:701 位置在 3 秒后更改和1米。 requestonLocationUpdates
-
请发布 lat1,lon1,lat2,lon2 的数字以及您认为错误的距离。可能你的公式有效。使用距离标尺工具在 Google 地球中输入它们。
标签: android geolocation gps geocoding geopositioning