登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
在业务上经常需要计算2个地点的距离,如台风路径预报误差。两点之间的距离实际上是球面距离,参考了网上搜索后,编写了VB源程序,供大家参考。yry,2012.5 Haikou,以下是我的新浪博客相关内容:
http://blog.sina.com.cn/s/blog_0347471501014zu8.html
- Const EARTH_RADIUS = 6378.137
- Const pi = 3.1415926535898
- Public IS_stationP As Boolean
- Private Sub Command1_Click()
- Dim lon1 As Double, lon2 As Double, lat1 As Double, lat2 As Double
- lon1 = Val(Text1)
- lat1 = Val(Text2)
- lon2 = Val(Text3)
- lat2 = Val(Text4)
- Lab_距离.Caption = "近似距离: " & Format(GetDistance(lat1, lon1, lat2, lon2), "####.0") & " 公里"
- End Sub
- Function rad(ByVal d As Double) As Double
- rad = d * pi / 180
- End Function
- '知道两点经纬度,求近似距离
- Public Function GetDistance(lat1 As Double, lng1 As Double, lat2 As Double, lng2 As Double) As Double
- Dim radlat1 As Double, radlat2 As Double
- Dim a As Double, b As Double, s As Double, Temp As Double
- radlat1 = rad(lat1)
- radlat2 = rad(lat2)
- a = radlat1 - radlat2
- b = rad(lng1) - rad(lng2)
- Temp = Sqr(Sin(a / 2) ^ 2 + Cos(radlat1) * Cos(radlat2) * Sin(b / 2) ^ 2)
- s = 2 * Atn(Temp / Sqr(-Temp * Temp + 1)) '没有反正弦函数,网上找了个
- s = s * EARTH_RADIUS
- GetDistance = s
- End Function
|