Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps API for Time and Distance Data (Access/VBA)

I have an application where it would be highly beneficial to be able to get directly from google maps the time and miles (driving miles) between two addresses. I have plowed through many web sites and documents and totally foxed as to how to do it.

Has anyone made a successful connection with google maps API using Microsoft Approach and extracted time and distance information? If have, o could they share the code they used.

I am trying to set up a driver scheduling system for a volunteer drivers group (driving for people wo medical appointments who cannot drive, cannot afford taxies and cannot afford cars!).

Any help would be appreciated

John Baker

like image 695
user1327936 Avatar asked Nov 20 '25 03:11

user1327936


1 Answers

Here's some code to do what you want to do. Do realize that Google does have some licensing requirements/limitations on using their API. You are supposed to use the results obtained here in a Google Map. Please read the Usage Limitations here: https://developers.google.com/maps/documentation/directions/

Note that this requires a reference to "Microsoft XML, v6.0" unless you change the code to use late binding.

Dim sXMLURL As String
sXMLURL = "http://maps.googleapis.com/maps/api/directions/xml?origin=NY,+NY&destination=Los+Angeles,+CA&sensor=false"

Dim objXMLHTTP As MSXML2.ServerXMLHTTP
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

Dim domResponse as DOMDocument60
Set domResponse = New DOMDocument60
domResponse.loadXML objXMLHTTP.ResponseText
Dim ixnStatus
Set ixnStatus = domResponse.selectSingleNode("//status")

If ixnStatus.Text = "OK" Then
    Dim ixnDistance, ixnDuration
    Set ixnDistance = domResponse.selectSingleNode("/DirectionsResponse/route/leg/distance/text")
    Set ixnDuration = domResponse.selectSingleNode("/DirectionsResponse/route/leg/duration/text")

    Debug.Print "Distance: " & ixnDistance.Text 'Miles
    Debug.Print "Duration: " & ixnDuration.Text 'Days Hours Minutes
End If

Set domResponse = Nothing
Set objXMLHTTP = Nothing

I've intentionally chosen to use XML instead of JSON because VBA is not overly handy with JSON, and neither am I. There is a module you can download often referred to as VBJSON but it has a little learning curve, especially if you never worked with JSON before. I believe XML is more "expensive" (less efficient) but I think it's also easier to work with in Access VBA.

like image 199
HK1 Avatar answered Nov 21 '25 21:11

HK1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!