我在http://analystcave.com/excel-calculate-distances-between-addresses/#Calculate_distance_between_two_addresses_using_Google_Maps_in_Excel上找到了以下代码
但是我想在我的Access DB中使用它从表单上的两个文本框中获取值,然后计算距离,并在单击命令按钮时将距离返回到表单上的第三个文本框中。当我使用代码时,它为使用fn Application.International(xlListSeparator)的那部分代码抛出一个错误。如何在Access中执行此操作?
'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?origins="
secondVal = "&destinations="
lastVal = "&mode=car&language=pl&sensor=false"
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发布于 2017-06-23 13:06:52
如果忽略Application.International(xlListSeparator)位,只使用:
tmpVal = matches (0).SubMatches (0)它在MS Access中为我做到了这一点。
https://stackoverflow.com/questions/38751817
复制相似问题