问题:
提供的 Java 代码片段计算两点之间的距离基于纬度和经度的两个点。令人担忧的是,所使用的公式可能会产生稍微不准确的结果,尤其是当有多个点距离很远时。
解决方案:
要解决此问题,请执行以下操作:半正矢方法的 Java 实现,也考虑了高度差:
<code class="java">/** * Calculate distance between two points in latitude and longitude taking * into account height difference. If you are not interested in height * difference pass 0.0. Uses Haversine method as its base. * * lat1, lon1 Start point lat2, lon2 End point el1 Start altitude in meters * el2 End altitude in meters * @returns Distance in Meters */ public static double distance(double lat1, double lat2, double lon1, double lon2, double el1, double el2) { final int R = 6371; // Radius of the earth double latDistance = Math.toRadians(lat2 - lat1); double lonDistance = Math.toRadians(lon2 - lon1); double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double distance = R * c * 1000; // convert to meters double height = el1 - el2; distance = Math.pow(distance, 2) + Math.pow(height, 2); return Math.sqrt(distance); }</code>
此实现准确计算距离,同时还考虑了高度差。它以半正矢方法为基础,同时考虑两点之间的高度差。
以上是如何利用纬度、经度、海拔高度准确计算两点之间的距离?的详细内容。更多信息请关注PHP中文网其他相关文章!