Google Maps Distance Matrix API transit mode doesn’t work? [Solved]

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 4.75 out of 5)
Loading...
Go back to All Questions Login or Register
Solved124 views
0

Hi, excellent article on calculating distances in VBA using the Google Maps Distance Matrix API.

I modified the lastVal string var in your GetDistance function slightly to change the travel mode to transit. Following the Google Maps Distance Maxtrix API documentation, I also supplied a transit_mode=subway parameter and value in the lastVal string.


'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=transit&transit_mode=subway&language=en&sensor=false"
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = firstVal & Replace(start, " ", "+") & secondVal & Replace(dest, " ", "+") & lastVal
etc...

The function is now not working, always returns -1. BTW, all the addresses are within New York City, where we have an extensive subway system.

Do you know what I’m doing wrong?

Thank you.

0

Seems this is a functionality limited to when you provide an API key or Client ID.
Run this link in the browser to see Google error message:
http://maps.googleapis.com/maps/api/distancematrix/json?origins=Chicago,US&destinations=Washington,US&mode=transit&transit_mode=subway&language=pl&sensor=false

This message is not thrown when using other modes of transport.

Setting up your API Key is free (as long as you don’t exceed the limits, then it is still very cheap – read here) and all you need to do to make the query work is append your API Key to the URL:

Simply the best place to learn VBA!

Error: Please enter a valid email address

Error: Invalid email

Error: Please enter your first name

Error: Please enter your last name

Error: Please enter a username

Error: Please enter a password

Error: Please confirm your password

Error: Password and password confirmation do not match