Use VBA to get step polylines from Google Directions API xml file

jameschristian

New Member
Joined
Aug 25, 2022
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hi all,
I currently use VBA to extract the 'overview polyline' returned from the Google Directions API, and paste it into a spreadsheet. However, I need more accurate polylines. I can get better accuracy if I can stitch together the child node polylines within each step.

How do I convert my code to go from retrieving the 'overview polyline' to retrieving the a string made up of the polylines from each 'step' from each 'leg'? There are variable (>1) legs and variable (>1) steps. I tried making a loop and just made a complete mess.

Please find attached an XML example. The VBA code is below.

VBA Code:
Set objHttp = New MSXML2.XMLHTTP60

With objHttp
    .Open "GET", strURL, False
    .setRequestHeader "Content-Type", "application/x-www-form-URLEncoded"
    .Send
End With
Set objDom = New DOMDocument60
objDom.LoadXML (objHttp.responseText)

Dim strStatus As String
strStatus = objDom.SelectSingleNode("//status").Text
If strStatus = "OK" Then
    ReDim aryDest(0, 1)
        Dim datanode As MSXML2.IXMLDOMNode
        Set datanode = objDom.SelectNodes("//route/overview_polyline")(0)
        
        If datanode.SelectNodes("//status")(0).Text = "OK" Then
            strpoly = datanode.SelectNodes("//route/overview_polyline")(0).Text 'This is where I have tried putting in a loop for each leg and each step, but it is a disaster when I do
            aryDest(0, 0) = strpoly 
        Else
            aryDest(0, 0) = datanode.SelectNodes("status")(0).Text
        End If

Else
    ReDim aryDest(0, 0)
    aryDest(0, 0) = "NO DATA"
End If
 

Attachments

  • Mr_Excel.png
    Mr_Excel.png
    77.5 KB · Views: 46

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
This shows how to loop through the legs and steps and get the polylines.

VBA Code:
'Requires reference to Microsoft XML v6.0

Option Explicit

Public Const APIkey = "YOUR_API_KEY"

Public Sub Test_Directions_API()

    #If VBA7 Then
        Dim XMLhttpReq As XMLHTTP60
        Set XMLhttpReq = New XMLHTTP60
    #Else
        Dim XMLhttpReq As XMLHTTP
        Set XMLhttpReq = New XMLHTTP
    #End If
    Dim status As IXMLDOMNode
    Dim legs As IXMLDOMNodeList, steps As IXMLDOMNodeList
    Dim thisLeg As IXMLDOMNode, thisStep As IXMLDOMNode
    Dim URL As String
    Dim origin As String, destination As String
      
    origin = "Your origin"
    destination = "Your destination"
 
    URL = "https://maps.googleapis.com/maps/api/directions/xml?origin=" & Application.WorksheetFunction.EncodeURL(origin) & "&destination=" & Application.WorksheetFunction.EncodeURL(destination) & "&key=" & APIkey
 
    With XMLhttpReq
        .Open "GET", URL, False
        .send
        Set status = .responseXML.SelectSingleNode("//DirectionsResponse/status")
        Set legs = .responseXML.SelectNodes("//route/leg")
    End With
  
    If status.Text = "OK" Then
 
        Debug.Print legs.Length
        For Each thisLeg In legs
            Set steps = thisLeg.SelectNodes("//step")
            Debug.Print steps.Length
            For Each thisStep In steps
                Debug.Print thisStep.SelectSingleNode("polyline").Text
            Next
        Next
     
    End If
 
End Sub
 
Upvote 0
Solution
Thanks @John_w that works nicely, I see how you did what you did.

Is there a way to get the concatenated polylines from each [step] put into one cell? That is, the result of all "for each thisStep In steps" printed in one cell?
 
Upvote 0
You could append them to a string.

Before the loop:
VBA Code:
Dim s As String
s = ""
Inside the inner loop:
VBA Code:
s = s & thisStep.SelectSingleNode("polyline").Text & ","
After the loop:
VBA Code:
Range("A1").Value = Left(s,Len(s)-1)
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,680
Members
449,463
Latest member
Jojomen56

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top