Results 1 to 27 of 27

Getting Distances and DriveTimes

Threaded View

  1. #1
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Getting Distances and DriveTimes

    Getting driving distances and drive times seems to be a very popular topic recently. I had originally posted a thread as to how to get this via the google API which is extremely simple, grabbing this and putting it into excel directly however breaches the terms of use.

    Since then, I have discovered that Microsoft also offers a similar API, you do however need to sign up and get a key (here) and accept their terms of use.

    I don't think posting the below contravenes anything, but if anyone feels otherwise let me know and I'll happily remove this.

    In the below, you need to input your API key in the relevant place.

    Function GetDistance(sPCode As String, ePcode As String) As Double
    
        Dim t As String
        Dim re As Object
        
        
        t = "http://dev.virtualearth.net/REST/V1/Routes/Driving?o=xml&wp.0=" & sPCode & "&wp.1=" & ePcode & "&avoid=minimizeTolls&du=mi&key=YOUR_MS_KEY"
        Set re = CreateObject("msxml2.xmlhttp")
        
        re.Open "get", t, False
        re.send
        Do
            DoEvents
        Loop Until re.readyState = 4
        
        With re
            s = Split(.responseText, "<TravelDistance>")
        End With
        
        GetDistance = Val(s(1))
    
    End Function
    
    Function GetTimeinMins(sPCode As String, ePcode As String) As Double
    
        Dim t As String
        Dim re As Object
        
        t = "http://dev.virtualearth.net/REST/V1/Routes/Driving?o=xml&wp.0=" & sPCode & "&wp.1=" & ePcode & "&avoid=minimizeTolls&du=mi&key=YOUR_MS_KEY"
        
        Set re = CreateObject("msxml2.xmlhttp")
        re.Open "get", t, False
        re.send
        Do
            DoEvents
        Loop Until re.readyState = 4
        
        With re
            s = Split(.responseText, "<TravelDuration>")
        End With
        
        GetTimeinMins = Val(s(1)) / 60
    
    End Function
    Last edited by dominicb; 05-31-2018 at 06:41 AM. Reason: Edited at Kyle's request to demonstrate late binding

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1