经过多天的整理,终于把这几个相互转换整理完成。
【说明】
谷歌地图API,高德地图API,腾讯地图API上取到的,都是GCJ-02坐标。
百度地图API是 BD-09坐标。
我们国家标准经纬度是WG1984坐标。
【代码】
高德经纬度转标准经纬度
Const PI = 3.1415926535897931 '圆周率
Function 高德转标准(st As String) As String
Try
Dim a As Double = 6378245.0
Dim ee As Double = 0.0066934216229659433
Dim _lon = CDbl(Split(st, ",")(0))
Dim _lat = CDbl(Split(st, ",")(1))
Dim dlon = 转经度(_lon - 105.0, _lat - 35.0)
Dim dlat = 转纬度(_lon - 105.0, _lat - 35.0)
Dim radlat = _lat / 180.0 * PI
Dim magic = Math.Sin(radlat)
magic = 1 - ee * magic * magic
Dim sqrtMagic = Math.Sqrt(magic)
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI)
dlon = (dlon * 180.0) / (a / sqrtMagic * Math.Cos(radlat) * PI)
Return (_lon - dlon) & "," & _lat - dlat
Catch
高德转标准 = ","
End Try
End Function
Function 转经度(x As Double, y As Double)
Dim ret As Double = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x))
ret += (20.0 * Math.Sin(6.0 * x * PI) + 20.0 * Math.Sin(2.0 * x * PI)) * 2.0 / 3.0
ret += (20.0 * Math.Sin(x * PI) + 40.0 * Math.Sin(x / 3.0 * PI)) * 2.0 / 3.0
ret += (150.0 * Math.Sin(x / 12.0 * PI) + 300.0 * Math.Sin(x / 30.0 * PI)) * 2.0 / 3.0
Return ret
End Function
Function 转纬度(x As Double, y As Double)
Dim ret As Double = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x))
ret += (20.0 * Math.Sin(6.0 * x * PI) + 20.0 * Math.Sin(2.0 * x * PI)) * 2.0 / 3.0
ret += (20.0 * Math.Sin(y * PI) + 40.0 * Math.Sin(y / 3.0 * PI)) * 2.0 / 3.0
ret += (160.0 * Math.Sin(y / 12.0 * PI) + 320 * Math.Sin(y * PI / 30.0)) * 2.0 / 3.0
Return ret
End Function
标准经纬度转高德经纬度
调用API,需要去高德地图官网申请key(暂时没找到其它方法,有其它方法的请留言告诉我)
Function 标准转高德(st As String) As String
标准转高德 = 转高德(st, "gps")
End Function
Function 转高德(st As String, res As String) As String '其他转高德,res=gps(标准转高德) =baidu(百度转高德)
Try
Dim url = "http://restapi.amap.com/v3/assistant/coordinate/convert?locations=" & st & "&coordsys=" & res & "&output=JSON&key=" & myKey '申请key
Dim winhttpReq As Object
Dim objJSON As Object
winhttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
With winhttpReq
.Open("GET", url, False)
.send()
Dim str = .RESPONSETEXT
If str <> "" Then
With CreateObject("msscriptcontrol.scriptcontrol")
.Language = "JavaScript"
.AddCode("var mydata =" & str)
objJSON = .CodeObject
转高德 = .Eval("mydata.locations")
End With
End If
End With
Catch
转高德 = ","
End Try
End Function
百度经纬度转标准经纬度
1、百度先转高德
2、高德再转为标准。
标准经纬度转百度经纬度
方法一:
1、标准先转高德
2、高德再转为百度
方法二(API):
Function 标准转百度(st As String) As String
Dim x, y, x1, y1
Dim from = "0" '原始
Dim tos = "4" '百度
Dim objJSON
'--------------------------------------------------------------------
'请求接口中有四个参数:
'其中,from和to对应的值分别是:0真实坐标;2google坐标;4baidu坐标。
'from: 被转换的坐标体系()
'to:转换到这个坐标体系
'x: 经度()
'y: 纬度()
'接口应答中有三个key,
'应答格式如:{"error":0,"x":"MTE2LjMyMTYyMzg4MjIz","y":"NDAuMDY2NDE2Mjc4NzMx"}
'--------------------------------------------------------------------
Dim url = "http://api.map.baidu.com/ag/coord/convert?from=" & from & "&to=" & tos & "&x=" & Split(st, ",")(0) & "&y=" & Split(st, ",")(1)
Dim winhttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
With winhttpReq
.Open("GET", url, False)
.send()
Dim str = .RESPONSETEXT
If str <> "" Then
With CreateObject("msscriptcontrol.scriptcontrol")
.Language = "JavaScript"
.AddCode("var mydata =" & str)
objJSON = .CodeObject
x = Convert.FromBase64String(.Eval("mydata.x"))
y = Convert.FromBase64String(.Eval("mydata.y"))
For i = 0 To UBound(x) : x1 = x1 & Chr(x(i)) : Next '解密经度
For i = 0 To UBound(y) : y1 = y1 & Chr(y(i)) : Next '解密纬度
End With
End If
标准转百度 = x1 & "," & y1
End With
End Function
百度经纬度转高德经纬度
Const PI = 3.1415926535897931 '圆周率
Function 百度转高德(st As String) As String
'百度转高德 = 转高德(st, "baidu")
Try
Dim x_pi As Double = PI * 3000.0 / 180.0
Dim x As Double = CDbl(Split(st, ",")(0)) - 0.0065
Dim y As Double = CDbl(Split(st, ",")(1)) - 0.006
Dim z As Double = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi)
Dim theta As Double = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi)
Dim x1 As Double = z * Math.Cos(theta)
Dim y1 As Double = z * Math.Sin(theta)
百度转高德 = CStr(x1 & "," & y1)
Catch
百度转高德 = ","
End Try
End Function
高德经纬度转百度经纬度
Const PI = 3.1415926535897931 '圆周率
Function 高德转百度(st As String) As String
Try
Dim x_pi As Double = PI * 3000.0 / 180.0
Dim x As Double = CDbl(Split(st, ",")(0))
Dim y As Double = CDbl(Split(st, ",")(1))
Dim z As Double = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi)
Dim theta As Double = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi)
Dim x1 As Double = z * Math.Cos(theta) + 0.0065
Dim y1 As Double = z * Math.Sin(theta) + 0.006
高德转百度 = CStr(x1 & "," & y1)
Catch
高德转百度 = ","
End Try
End Function