3

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

4

1 回答 1

8

这里有一些代码可以做你想做的事情。确实意识到谷歌在使用他们的 API 上有一些许可要求/限制。您应该在 Google 地图中使用此处获得的结果。请在此处阅读使用限制:https ://developers.google.com/maps/documentation/directions/

请注意,除非您更改代码以使用后期绑定,否则这需要引用“Microsoft XML, v6.0”。

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

我故意选择使用 XML 而不是 JSON,因为 VBA 与 JSON 并不太方便,我也不是。有一个模块可以下载,通常称为 VBJSON,但它有一点学习曲线,特别是如果你从未工作过之前使用 JSON。我相信 XML 更“昂贵”(效率较低),但我认为在 Access VBA 中使用它也更容易。

于 2012-04-12T16:55:40.650 回答