Excel中的Google Api用于计算地点之间的驾驶距离
我有大约20K对来计算它们之间的行驶距离。我正在使用下面的VB脚本-Excel中的Google Api用于计算地点之间的驾驶距离,excel,google-maps-api-3,vba,Excel,Google Maps Api 3,Vba,我有大约20K对来计算它们之间的行驶距离。我正在使用下面的VB脚本- 'Calculate Google Maps distance between two addresses Public Function GetDistance(start As String, dest As String) Dim firstVal As String, secondVal As String, lastVal As String firstVal = "http://maps.googl
'Calculate Google Maps distance between two addresses
Public Function GetDistance(start As String, dest As String)
Dim firstVal As String, secondVal As String, lastVal As String
firstVal = "http://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins="
secondVal = "&destinations="
lastVal = "&mode=car&language=en&sensor=false&key=A***Bh*Eh-g***LvJ7bRirvjlr****OkUvs"
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Url = firstVal & Replace(start, " ", "+") & secondVal & Replace(dest, " ", "+") & lastVal
objHTTP.Open "GET", Url, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.send ("")
If InStr(objHTTP.responseText, """distance"" : {") = 0 Then GoTo ErrorHandl
Set regex = CreateObject("VBScript.RegExp"): regex.Pattern = """value"".*?([0-9]+)": regex.Global = False
Set matches = regex.Execute(objHTTP.responseText)
tmpVal = Replace(matches(0).SubMatches(0), ".", Application.International(xlListSeparator))
GetDistance = CDbl(tmpVal)
Exit Function
ErrorHandl:
GetDistance = -1
End Function
当我写
=GetDistance(“亚特兰大”、“迈阿密”)
时,它抛出-1
(错误)。但是,当我在浏览器中粘贴URL时,它会工作 我也有同样的错误,但我找到了另一种方法(您需要激活参考Microsoft XML,v6.0):
资料来源:
使用您的代码生成结果:
第二个函数的结果:
我也有同样的错误,但我找到了另一种方法(您需要激活参考Microsoft XML,v6.0): 资料来源: 使用您的代码生成结果: 第二个函数的结果:
在您的代码中,您正在使用http,而API现在只接受https,您需要做的就是用https替换http
firstVal = "https://maps.googleapis.com/maps/api/distancematrix/json?origins="
在您的代码中,您正在使用http,而API现在只接受https,您需要做的就是用https替换http
firstVal = "https://maps.googleapis.com/maps/api/distancematrix/json?origins="
有
ErrorHandl:
@AntiDrondert当objHTTP.responseText与模式不匹配时有一个显式Goto(argh)。responseText的内容是什么?有ErrorHandl:
@AntiDrondert当objHTTP.responseText与模式不匹配时有一个显式Goto(argh)。responseText的内容是什么?