Of course this can be changed to get KM, time etc
Code:
'gets google value of time distance in seconds
Public Function googleTravelTime(AddressOrigin As String, AddressDestination As String) As Long
Dim objHttp As Object
Dim strURL As String, strDisType As String, strHTML As String, strOrigin As String, strDest As String
Dim lTopicstart As Long, lTopicend As Long
Dim strDist As String, strDura As String, lngDuraV As String
If IsMissing(AddressOrigin) Then
strOrigin = ""
Else
strOrigin = AddressOrigin
End If
If IsMissing(AddressDestination) Then
strDest = ""
Else
strDest = AddressDestination
End If
On Error GoTo GoogleError:
strDisType = "metric"
'GMap webservice with XML output
strURL = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins="
strURL = strURL & URLEncode(strOrigin) & "&destinations=" & URLEncode(strDest)
strURL = strURL & "&units=" & strDisType & "&sensor=false"
Set objHttp = CreateObject("MSXML2.XMLHTTP")
objHttp.Open "GET", strURL, False
objHttp.send
strHTML = objHttp.responseText
Set objHttp = Nothing
Debug.Print strHTML
'get seconds
'Parse the xml to get the Duration (Time value)
lTopicstart = InStr(strHTML, "<duration>")
lTopicend = InStr(strHTML, "</duration>")
strDuraV = Trim(Mid(strHTML, lTopicstart + 10, lTopicend - lltopicstart - 10))
'Debug.Print Len(strDura) & "[" & strDura & "]"
lTopicstart = InStr(strDuraV, "<value>")
lTopicend = InStr(strDuraV, "</value>")
strDuraV = Mid(strDuraV, lTopicstart + 7, lTopicend - lTopicstart - 7)
googleTravelTime = CLng(strDuraV)
'in seconds
'Debug.Print googleTravelTime
Exit Function
GoogleError:
MsgBox "error with google"
Exit Function
End Function