Call the Google Maps DistanceMatrix API from VBA

I'm attempting to create a procedure which will return the distance between two points based on the Google Maps DistanceMatrix API.  My initial code (VBA) looks like:
Public Function GoogleDistance(FromAddress As String, ToAddress As String) As String

    Dim strMyKey as string 
    Dim sXMLURL As String
    Dim objXMLHTTP As MSXML2.ServerXMLHTTP
    
    On Error GoTo ProcError
    
    strMyKey = "aaaa" '<= this is not the actual key
    sXMLURL = "https://maps.googleapis.com/maps/api/distancematrix/xml?" _
            & "origin=" & Replace(Replace(FromAddress, "  ", " "), " ", "+") & "&" _
            & "destination=" & Replace(Replace(ToAddress, "  ", " "), " ", "+") & "&" _
            & "key=" & strKey
'            & "sensor=False" & "&"
Debug.Print sXMLURL

    Set objXMLHTTP = New MSXML2.ServerXMLHTTP
    
    With objXMLHTTP
        .Open "Get", sXMLURL, False
        .setRequestHeader "content-Type", "application/x-www-form-URLEncoded"
        .send
    End With
    
Debug.Print objXMLHTTP.responseText

ProcExit:
    Exit Function
    
ProcError:
    Debug.Print Err.Number, Err.Description
    MsgBox Err.Number & vbCrLf & Err.Description
    Resume ProcExit

End Function

Open in new window

At this point, I'd be happy if this would return anything other than:
<?xml version="1.0" encoding="UTF-8"?>
<DistanceMatrixResponse>
  <status>INVALID_REQUEST</status>
</DistanceMatrixResponse>

Open in new window

I'm sure it is something simple in the formatting of the request string, maybe a comma, a space or something else.  The data I'm testing it with generates the following string:
https://maps.googleapis.com/maps/api/distancematrix/xml?origin=Grafton,+VA&destination=Yorktown,+VA&key=aaaa

Open in new window

LVL 51
Dale FyeOwner, Developing Solutions LLCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
In some cases you have to use "&20" for spaces (like the space in front of "VA").
0
Dale FyeOwner, Developing Solutions LLCAuthor Commented:
Scott,

Tried that and "%20" in lieu of the "+" to replace spaces in the request.  Still getting the INVALID_REQUEST message.

Dale
0
Dale FyeOwner, Developing Solutions LLCAuthor Commented:
Turns out I needed to use "origins" and "destinations" (plural) rather than "Origin" and "Destination"

Dale
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Google

From novice to tech pro — start learning today.