gpt4 book ai didi

algorithm - 计算两个经纬度点之间的距离? (哈弗斯公式)

转载 作者:行者123 更新时间:2023-11-30 05:33:11 25 4
gpt4 key购买 nike

如何计算由纬度和经度指定的两点之间的距离?

为了澄清,我想要以公里为单位的距离;这些点使用 WGS84 系统,我想了解可用方法的相对准确性。

最佳答案

link可能对您有帮助,因为它详细介绍了 Haversine formula 的使用计算距离。

摘录:

This script [in Javascript] calculates great-circle distances between the two points – that is, the shortest distance over the earth’s surface – using the ‘Haversine’ formula.

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}

function deg2rad(deg) {
return deg * (Math.PI/180)
}

关于algorithm - 计算两个经纬度点之间的距离? (哈弗斯公式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34864986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com