Google Maps API and Excel---Works perfect except one field not sure why

dumbwithdata

New Member
Joined
May 26, 2017
Messages
3
I have been muddling together calls to Google Maps API with an Excel sheet and after lots of testing and borrowing samples from other peoples projects (that I do not quite understand) I have the whole package working perfectly. Basically the workbook has multiple sheets and each of these sheets has one destination and about 30 origins. The call goes out to Google for all 30 of these and the Excel looks the duration value for each. Well I have altered the call to get the 'duration_in_traffic' value as opposed to the duration value. However, I cannot get these results to appear in my sheet. If I change this to distance rather than duration_in_traffic it will return the value fine. I am lost as to what the issue is. I have tried to troubleshoot based on my limited knowledge and:
  • If I make the call straight in Chrome, it comes back with all of the correct data so I know that this element is fine.
  • I changed to distance item so I know that it is not some strange naming or the like that needs to be changed in the VBA.
  • I tried multiple iterations with the 'duration_in_traffic' to see if some standard I was missing.
  • I reviewed the XML results that come back when you call in Chrome and I cannot see any structural difference that would make the call for distance and duration the same but duration_in_traffic different.

The only thing I can think of, which is related to that last point, is that the 'duration_in_traffic' value is only provided one in a call whereas the distance and duration values are provided multiple times for each step and then a total one which is the one we grab, but not sure about this. This is what the relevant portion of the current call looks like:

Code:
Set myDomDoc = New DOMDocument60
    myDomDoc.LoadXML myRequest.responseText
    
    ' If statusNode is "OK" then get the values to return
    Set statusNode = myDomDoc.SelectSingleNode("//status")
    If statusNode.Text = "OK" Then
        If NoCache Then: Call CreateFile(CachedFile, myRequest.responseText) ' Cache API response if required
        ' Get the duration
        Set durationNode = myDomDoc.SelectSingleNode("//leg/duration/value")
        If Not durationNode Is Nothing Then G_DURATION = val(durationNode.Text) / 60 ' Now works with comma as decimal separator
    End If

Thank you for any help that you can provide and I am hoping that the issue is really only because I am doing cut and paste code with no real clue. Thank you.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
are you using the latest api. google has a habit of changing it which causes stuff to fail
 
Upvote 0
I believe I am. The original project that I borrowed most of the code for was using a call that did not require a key, but since I added the departure time, traffic model, and duration_in_traffic I had to go get a key for it so would assume all up to date. In addition, all of the data comes back perfect when I make the call straight from Chrome and review the results in Chrome so I assume the call is fine and I am just messing up something bringing it into Excel. I think there is something wrong with the syntax that I am using, but I know just enough to be dangerous and get myself in trouble. This line is the original and works fine:

Code:
SetdurationNode = myDomDoc.SelectSingleNode("//leg/duration/value")

And if I change it to this it also works fine:

Code:
SetdurationNode = myDomDoc.SelectSingleNode("//leg/distance/value")

But it does not work if I try this:

Code:
SetdurationNode = myDomDoc.SelectSingleNode("//leg/duration_in_traffic/value")

All of these have the same data types, so I am assuming that my problem is with the syntax I am using which I am guessing is caused by either fact that duration_in_traffic (DIT) is (1) on a different level of the hierarchy and they needs to be written differently or (2) possibly the underscore character is to blame...I am lost but these are my best guesses.

Thank you for responding so promptly.
 
Upvote 0
is the place the data is coming from accessible for anyone, just wondering what the URL is
 
Upvote 0
The data engine is available to anyone, though if you also want the parameters I mentioned above you need to get a key which is free with quote restrictions.

This is the URL with all the green/bold text being parameters that you pass to it. The resulting page is your results in either XML or JSON

https://maps.googleapis.com/maps/api/directions/xml?units=imperial&avoid=tolls&departure_time=1501572600&traffic_model=pessimistic& origin=Chicago&destination=Detroit&mode=driving&language=en-EN&key=YOURKEYYOURKEYYOURKEYYOURKEY
 
Upvote 0

Forum statistics

Threads
1,223,793
Messages
6,174,624
Members
452,575
Latest member
Fstick546

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