计算位置之间的距离


Calculating distance between locations

我使用havershine公式计算位置之间的距离。

$distance = (((acos(sin(($fromLat * pi() / 180)) * sin(($toLat * pi( )/ 180)) + cos(($fromLat * pi() / 180)) * cos(($toLat * pi() / 180)) * cos((($fromLon - $toLon) * pi() / 180)))) * 180 / pi()) * 60 * 1.1515);

它工作正常,但对于某些坐标值,它给出的是NAN

它为NAN提供的值是

fromLat : 22.303894
fromLon : 70.802162
toLat : 22.303894
toLon : 70.802162

您正在比较相同的坐标。acos函数被要求计算1的反余弦,但失败了。在将坐标传递给函数之前,您需要检查坐标是否相同。此外,如果你没有得到一个数字,结果可能是没有距离。