在VBA已知纬度和经度的情况下,计算两点之间的方位角

时间:2016-08-03 10:01:35

标签: vba function excel-vba excel

我必须计算纬度和经度给出的两点之间的方位角 这个fynction是正确的吗?

Function azimut(lat1, lat2, lon1, lon2)

azimut = WorksheetFunction.Degrees(WorksheetFunction.Atan2(
   Cos(Application.WorksheetFunction.Radians(lat1)) * 
   Sin(Application.WorksheetFunction.Radians(lat2)) -
   Sin(Application.WorksheetFunction.Radians(lat1)) * 
   Cos(Application.WorksheetFunction.Radians(lat2)) * 
   Cos(Application.WorksheetFunction.Radians(lon2 - lon1)), 
   Sin(Application.WorksheetFunction.Radians(lon2 - lon1)) * 
   Cos(Application.WorksheetFunction.Radians(lat2))))

End Function

1 个答案:

答案 0 :(得分:2)

假设你的公式是正确的(因为我将其解释为下面的代码而不检查它),那么这里是代码:

Function Azimuth(lat1 As Single, lat2 As Single, lon1 As Single, lon2 As Single) As Single

Dim X1 As Single, X2 As Single, Y As Single, dX As Single, dY As Single

With Application.WorksheetFunction
    X1 = .Radians(lat1)
    X2 = .Radians(lat2)
    Y = .Radians(lon2 - lon1)
End With

    dX = Math.Cos(X1) * Math.Sin(X2) - Math.Sin(X1) * Math.Cos(X2) * Math.Cos(Y)
    dY = Math.Cos(X2) * Math.Sin(Y)


With Application.WorksheetFunction
    Azimuth = .Degrees(.Atan2(dX, dY))
End With

End Function

好吧,即使公式结果不正确,至少上面的代码应该给你一个开头的想法。