两个Lat / Longs之间的距离公式自动?

时间:2016-05-27 13:39:16

标签: trigonometry autoit

我试图计算两个坐标之间的距离,我遇到了麻烦。我已经看过JavaScript的公式,但似乎无法正确地将其转换为自动。示例

$lat1 = 34.521630
$lon1 = -119.134680

$lat2 = 34.911463
$lon2 = -119.453130

;
;
;  $distance = Some code here that will calculate distance
;
;
;

1 个答案:

答案 0 :(得分:1)

这不是AutoIt本身的问题,你需要正确的算法:

#include <Math.au3>

$lat1 = 34.521630
$lon1 = -119.134680

$lat2 = 34.911463
$lon2 = -119.453130

ConsoleWrite(_distanceInKm($lat1, $lon1, $lat2, $lon2) & @CRLF)

Func _distanceInKm($lat1, $lon1, $lat2, $lon2)
    Local $iRadius = 6371

    Local $iLat = _Radian($lat2 - $lat1)
    Local $iLon = _Radian($lon2- $lon1)

    Local $a = Sin($iLat / 2) * Sin($iLat / 2) + Cos(_Radian($lat1)) * Cos(_Radian($lat2)) * Sin($iLon / 2) * Sin($iLon / 2)
    Local $c = 2 * ATan2(Sqrt($a), Sqrt(1 - $a))
    Local $d = $iRadius * $c

    Return Abs($d)
EndFunc

Func ATan2($y, $x)
    Return (2 * ATan($y / ($x + Sqrt($x * $x + $y * $y))))
EndFunc